2

I have a title label in the navbar using the code below. I can't seem to get it to size to the width of the full view. Any idea how I might go about fixing this?

I thought self.view.bounds.size.width would size it to the full width of the view.

here's my navbar title label code and there's also a screenshot to visually show the problem.

thanks for the help

 UILabel* tlabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, self.view.bounds.size.width, 48)];
tlabel.text=self.navigationItem.title;
tlabel.text = @"someText"; 
tlabel.font = [UIFont fontWithName:@"DIN-Bold" size:20];
tlabel.textColor=[UIColor whiteColor];
tlabel.backgroundColor = [UIColor colorWithRed:(219/255.0) green:(52/255.0) blue:(31/255.0) alpha:1] ;
tlabel.adjustsFontSizeToFitWidth=YES;
tlabel.textAlignment = UITextAlignmentCenter;
self.navigationItem.titleView=tlabel;

enter image description here

after code change from Paras Joshi it's correct on the right side but off on the left.

enter image description here

hanumanDev
  • 6,592
  • 11
  • 82
  • 146

3 Answers3

1

Use this bellow code in which I've added UILabel in UIView and then set that view as a titleView...

UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 48)];
UILabel* tlabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 320, 48)];
tlabel.text=self.navigationItem.title;
tlabel.text = @"someText"; 
tlabel.font = [UIFont fontWithName:@"DIN-Bold" size:20];
tlabel.textColor=[UIColor whiteColor];
tlabel.backgroundColor = [UIColor colorWithRed:(219/255.0) green:(52/255.0) blue:(31/255.0) alpha:1] ;
tlabel.textAlignment = UITextAlignmentCenter;   
[headerView addSubview:tlabel];
self.navigationItem.titleView = headerView;

or add as a subview like below.

[self.navigationController.navigationBar addSubview:tlabel];
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • I do have some text in the @""; I just removed it for this post. I need that text there though – hanumanDev Aug 02 '13 at 10:53
  • @hanumanDev also try my updated code instead of your code.. :) – Paras Joshi Aug 02 '13 at 10:56
  • thanks! it's almost perfect. The right side is flush to the end of the view with your code. the left side however is still a few pixels off like in the screenshot. – hanumanDev Aug 02 '13 at 10:58
  • ok then take frame of that view or lable with width 324 try it out – Paras Joshi Aug 02 '13 at 11:01
  • I tried the 324 in the header view width and then also in the label but it's still off. I added a screenshot to illustrate. thanks for all the help :) – hanumanDev Aug 02 '13 at 11:08
  • 1
    @hanumanDev check out my another answer from this link http://stackoverflow.com/questions/17341575/setting-title-for-uinavigation-bar-in-iphone/17341896#17341896 also then try to add as a subview instead of set as a titleview like this `[self.navigationController.navigationBar addSubview:label];` – Paras Joshi Aug 02 '13 at 11:11
  • solved it with [self.navigationController.navigationBar addSubview:label]; thanks again :) – hanumanDev Aug 02 '13 at 11:14
0

Try it with:

UILabel* tlabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, [UIScreen mainscreen].bounds.size.width, 48)];

May be it works.

Pradhyuman sinh
  • 3,936
  • 1
  • 23
  • 38
0

you have imported uikit framework or not.import it to make it error free

NHS
  • 409
  • 2
  • 7