I have created a application which displays the contacts in my device in a table view and when i click a row it will display the details of that contact on another view. This is how the storyboard looks like:
In my didSelectRowAtIndexPath i have this code:
NewViewController *WVC = [self.storyboard instantiateViewControllerWithIdentifier:#"NewViewController"];
[self.navigationController pushViewController:WVC animated:YES];
When i run the application, the app looks like:
However when i click on a row the details are displayed, but like this:
Can someone let me know where i am going wrong, thanks in advance.
Code in NewViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 300, 20)];
mylabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 125, 300 , 20)];
[nameLabel setText:#"Name :"];
[mylabel setText:_LabelText];
[self.view addSubview:nameLabel];
[self.view addSubview:mylabel];
myPhonelabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 175, 300, 20)];
[myPhonelabel setText:#"Contact Number :"];
PhoneLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 200, 300, 20)];
[PhoneLabel setText:_phoneNumber];
[self.view addSubview:myPhonelabel];
[self.view addSubview:PhoneLabel];
myEmailLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 240, 300, 20)];
[myEmailLabel setText:#"Email ID :"];
EmailLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 265, 300, 20)];
[EmailLabel setText:_emailId];
[self.view addSubview:myEmailLabel];
[self.view addSubview:EmailLabel];
myBdayLabel =[[UILabel alloc]initWithFrame:CGRectMake(10, 315, 300, 20)];
[myBdayLabel setText:#"Date Of Birth :"];
BdayLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 335, 300, 20)];
[BdayLabel setText:_birthDate];
[self.view addSubview:myBdayLabel];
[self.view addSubview:BdayLabel];
myButton = [[UIButton alloc]initWithFrame:CGRectMake(10, 30, 300, 30)];
[myButton setTitle:#"Contact Details" forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
myButton.backgroundColor = [UIColor colorWithRed:0xBD/255.0 green:0xBE/255.0 blue:0xC2/255.0 alpha:1];
myButton.layer.cornerRadius = 10;
myButton.clipsToBounds = YES;
[self.view addSubview:myButton];
}
EDIT: (ANSWER)
After a lot of head banging and hair pulling, the problem was solved by replacing
NewViewController *WVC = [self.storyboard instantiateViewControllerWithIdentifier:#"NewViewController"];
with
NewViewController *WVC = [[NewViewController alloc] init];
Honestly i don't know why the previous one didn't work.
If you don't need navigation bar, use to hide it
[self.navigationController setNavigationBarHidden:YES];
Related
I am developing one application for iphone and in that i have some issues regarding adding more then one UILabel in navigation bar at a specific position.
What i want is in following images
in above image there is one backbar button and one imageview as shown with arrow-mark. Other then that all white box is for difrent UILabels to show in navigation bar.
There is only one UIImageView in the navigation bar and one left bar button. Now problem is that i don't know how to add multiple UILabel in navigation bar at a specific position.
Till now what i have worked is to add only button and UIImageView with the right bar button array or left bar button array, but that only add items in sequnce.
So can anyone please guide me that how can anyone add UILabels or any other item at a specific position..
you can add Multiple UIlabel or Image as look like bellow image:-
this can do using bellow code you can change Label and imageView frame and put as your requirement
- (void)viewDidLoad
{
UIView *btn = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
UILabel *label;
label = [[UILabel alloc] initWithFrame:CGRectMake(5, 25, 200, 16)];
label.tag = 1;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:16];
label.adjustsFontSizeToFitWidth = NO;
label.textAlignment = UITextAlignmentLeft;
label.textColor = [UIColor whiteColor];
label.text = #"My first lable";
label.highlightedTextColor = [UIColor whiteColor];
[btn addSubview:label];
[label release];
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 200, 16)];
label.tag = 2;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:16];
label.adjustsFontSizeToFitWidth = NO;
label.textAlignment = UITextAlignmentRight;
label.textColor = [UIColor whiteColor];
label.text = #"second line";
label.highlightedTextColor = [UIColor whiteColor];
[btn addSubview:label];
[label release];
UIImageView *imgviw=[[UIImageView alloc]initWithFrame:CGRectMake(170, 10, 20, 20)];
imgviw.backgroundColor = [UIColor blackColor];
imgviw.image=[UIImage imageNamed:#"a59117c2eb511d911cbf62cf97a34d56.png"];
[btn addSubview:imgviw];
self.navigationItem.titleView = btn;
}
You can add subviews directly to the navigationBar -
UINavigationBar *navBar = navController.navigationBar;
[navBar addSubview: yourLabel];
yourLabel frame origin will be relative to the navigation bar
Try this code
UIView *buttonContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
buttonContainer.backgroundColor = [UIColor clearColor];
UIButton *button0 = [UIButton buttonWithType:UIButtonTypeCustom];
[button0 setFrame: CGRectMake(160, 7 , 40, 30)];
[button0 setTitle:#"Sort" forState:UIControlStateNormal];
button0.titleLabel.font = [UIFont fontWithName:#"Helvetica" size:12];
[button0 setBackgroundImage:[UIImage imageNamed:#"Btn_Sort_Btn_Sort_Places.png"] forState:UIControlStateNormal];
[button0 addTarget:self action:#selector(sortAction) forControlEvents:UIControlEventTouchUpInside];
[button0 setShowsTouchWhenHighlighted:YES];
[buttonContainer addSubview:button0];
self.navigationItem.titleView = buttonContainer;
UILabel *lbl_title = [[UILabel alloc] initWithFrame:CGRectMake(10, 0 , 140, 40)];
lbl_title.text = str_HeaderTitle;
lbl_title.textColor = [UIColor whiteColor];
lbl_title.font = [UIFont fontWithName:#"Helvetica-Bold" size:16];
lbl_title.backgroundColor = [UIColor clearColor];
lbl_title.textAlignment = NSTextAlignmentCenter;
[buttonContainer addSubview:lbl_title];
You can set an arbitrary view instead of a view controller's title:
myViewController.navigationItem.titleView = someView;
Note that most likely the view's frame will be restricted to make room for leftBarButtonItem and rightBarButtonItem.
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(50, 2, 20, 15)];
[label setBackgroundColor:[UIColor greenColor]];
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(40, 20, 150, 10)];
[imgView setBackgroundColor:[UIColor redColor]];
[[self.navigationController navigationBar] addSubview:label];
[self.navigationController.navigationBar addSubview:imgView];
Have a look at this piece of code. With some modifications it should solve your problem:
UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
[imageButton setFrame:CGRectMake(15, 0, 57, 44)];
[imageButton setBackgroundImage:[UIImage imageNamed:#"someImage.png"] forState:UIControlStateNormal];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 0, 250, 44)];
[titleLabel setText:#"some title"];
[titleLabel setBackgroundColor:[UIColor clearColor]];
[titleLabel setTextColor:[UIColor whiteColor]];
[titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
[self.navigationController.navigationBar addSubview:imageButton];
[self.navigationController.navigationBar addSubview:titleLabel];
Just modify the values in the CGRectMake() to fit your needs.
Hi i am beginner in iOS and in my project i need to insert "titles" at "left side center" and "right side center" on UInavigationBar as like below image please help me how should i do below my requirement
See below code and try :
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = [[UIScreen mainScreen] bounds];
UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width/2, 44)];
lbl1.backgroundColor = [UIColor redColor];
lbl1.textAlignment = NSTextAlignmentCenter;
lbl1.text = #"Dashboard";
[self.navigationController.navigationBar addSubview:lbl1];
UILabel *lbl2 = [[UILabel alloc] initWithFrame:CGRectMake(frame.size.width/2, 0, frame.size.width/2, 44)];
lbl2.backgroundColor = [UIColor redColor];
lbl2.textAlignment = NSTextAlignmentCenter;
lbl2.text = #"Profile";
[self.navigationController.navigationBar addSubview:lbl2];
}
You can customize the titleView of navigation bar. Read the official documentation here.
As per your question, I tried the below working code:-
-(UIView *)getTitleView
{
CGRect frame = [[UIScreen mainScreen] bounds];
UIView *viewTitle = [[UIView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, self.navigationController.navigationBar.frame.size.height)];
UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width/2, self.navigationController.navigationBar.frame.size.height)];
lbl1.backgroundColor = [UIColor colorWithRed:130/255.0f green:13/255.0f blue:23/255.0f alpha:1.0f];
lbl1.textAlignment = NSTextAlignmentCenter;
[lbl1 setTextColor:[UIColor whiteColor]];
lbl1.text = #"Dashboard";
[viewTitle addSubview:lbl1];
UILabel *lbl2 = [[UILabel alloc] initWithFrame:CGRectMake(frame.size.width/2, 0, frame.size.width/2, viewTitle.frame.size.height)];
lbl2.backgroundColor = [UIColor colorWithRed:130/255.0f green:13/255.0f blue:23/255.0f alpha:1.0f];
[lbl2 setTextColor:[UIColor whiteColor]];
lbl2.textAlignment = NSTextAlignmentCenter;
lbl2.text = #"Profile";
[viewTitle addSubview:lbl2];
return viewTitle;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.titleView = [self getTitleView];
}
I divide the code in separate method to follow modular approach. For any part of code, which is of separate task, write that piece of code in different method. This will lead to more readability.
I have following code that adds a UIView and then adds a UIImageView and UILabel, the UIView display correctly but nothing i have put in side the view is showing. I have looked everywhere but i cannot find an answer. The UIView is presented under certain conditions and when it is tapped it dissapears.
Here is my code.
UIView *actionView = [[UIView alloc] initWithFrame:CGRectMake(20, 180, 280, 165)];
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[actionView addGestureRecognizer:singleFingerTap];
actionView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f];
actionView.layer.cornerRadius = 5;
actionView.layer.masksToBounds = YES;
[self.view addSubview:actionView];
UILabel *actionText = [[UILabel alloc] initWithFrame:CGRectMake(20, 180, 280, 165)];
actionText.text = #"Hello";
actionText.textColor = [UIColor redColor];
actionText.textAlignment = NSTextAlignmentCenter;
[actionText setTextColor:[UIColor redColor]];
[actionText setBackgroundColor:[UIColor clearColor]];
[actionText setFont:[UIFont fontWithName: #"Trebuchet MS" size: 14.0f]];
actionText.Frame = CGRectMake(20, 180, 280, 165);
[actionView addSubview:actionText];
UIImage* image = [UIImage imageNamed:[NSString stringWithFormat:#"trash-icon.png"]];
UIImageView* blockView = [[UIImageView alloc] initWithImage:image];
blockView.frame = CGRectMake(109, 273, 40, 40);
[actionView addSubview:blockView];
Change your frame positions like..
UILabel *actionText = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 280, 40)];
actionText.text = #"Hello";
actionText.textColor = [UIColor redColor];
actionText.textAlignment = NSTextAlignmentCenter;
[actionText setTextColor:[UIColor redColor]];
[actionText setBackgroundColor:[UIColor clearColor]];
[actionText setFont:[UIFont fontWithName: #"Trebuchet MS" size: 14.0f]];
[actionView addSubview:actionText];
UIImage* image = [UIImage imageNamed:[NSString stringWithFormat:#"trash-icon.png"]];
UIImageView* blockView = [[UIImageView alloc] initWithImage:image];
blockView.frame = CGRectMake(109,70, 40, 40);
[actionView addSubview:blockView];
When you set the frame of a view, it is important to remember that the origin relates to the coordinate system of the superview it is added to.
So when you set the frame of your label to:
UILabel *actionText = [[UILabel alloc] initWithFrame:CGRectMake(20, 180, 280, 165)];
the origin of x = 20, y = 180 end up being the origin WITHIN the actionView. Now this is your problem because your actionView is only 165px tall, and you are telling the actionText subView that it's y origin is 180, which is well outside the bottom of the actionView.
for your actionText, you should allocate it as follows:
UILabel *actionText = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 280, 165)];
and for your blockView, something like:
blockView.frame = CGRectMake(89,93, 40, 40);
I have a simple but custom section header in which I am showing dynamic content in a label. However, when I pop a VC and in the active VC I run [self.quickDialogTableView reloadData] in viewWillAppear, the data is not being updated to the correct "self.sectionHeaderTitle. Any idea what I am doing wrong?
(void)sectionHeaderWillAppearForSection:(QSection *)section atIndex:(NSInteger)indexPath {
if (indexPath == 0) {
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 5, 320, 110)];
header.backgroundColor = [UIColor clearColor];
UIView *header_background = [[UIView alloc] initWithFrame:CGRectMake(25, 10, 270, 100)];
header_background.backgroundColor = [UIColor whiteColor];
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 255, 25)];
title.text = #"A sample static title";
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(55, 40, 200, 40)];
label.text = self.sectionHeaderTitle;
[header addSubview:header_background];
[header_background addSubview:title];
[header_background addSubview:label];
[section setHeaderView:header];
}
Try reload the section, like this:
[self.quickDialogTableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationBottom];
myscrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.height)];
myscrollview.delegate = self;
[self.view addSubview:myscrollview];
CGSize scrollViewContentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height+500);
[myscrollview setContentSize:scrollViewContentSize];
lblluckyno = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 200, 50)];
lblfirstmsg = [[UILabel alloc] initWithFrame:CGRectMake(15, 50, 295, 800)];
But I don't know My Firstmasg length.... after that lblfirstmsg I want to add second lblsecongmgs.... So how can I add?
After you set first label and set it content you can call [lblfirstmsg sizeToFit]; to fit frame to its content. Then you can add your second UIlabel next to the first one [[UILabel alloc] initWithFrame:CGRectMake(lblfirstmsg.frame.origin.x + lblfirstmsg.frame.size.width, lblfirstmsg.frame.origin.y, 200, 50)];
[myscrollview addSUbview:lblluckyno];
and same way add 2nd label.
This is your scroll view code.
myscrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.height)];
myscrollview.delegate = self;
[self.view addSubview:myscrollview];
[myscrollview setContentSize:CGSizeMake(self.view.frame.size.width,self.view.frame.size.height+500);];
After you set two label With CGRect and add in Scroll View
[myScrollView addSubView:lblluckyno];
......
Hello Mayur P Bhansali,
You can find the position and dimension of your first label like so:
lblluckyno = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 200, 50)];
UILabel *secondLabel = [[UILabel alloc] initWithFrame: CGRectMake(lblluckyno.frame.origin.x + lblluckyno.frame.size.width, 200, 100)];
Each UIView has a "frame" object and this frame object has a "origin" C structure and "size" C structure. Using labelName.frame.size.width gives you the width of a frame and labelName.frame.origin.x gives you the X coordinate of the frame.
You could access these properties like this too:
[labelName frame].size.width
[labelName frame].size.origin.x
lblluckyno = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 200, 50)];
lblfirstmsg = [[UILabel alloc] initWithFrame:CGRectMake(15, 50, 295, 800)];
[myscrollview addSUbview:lblluckyno];
[myscrollview addSUbview:lblfirstmsg];