-1

Im having trouble setting a programmatically allocated UILabel to be centered in my NavigationItem's titleView. Im even setting the NSAlignment to be NSAlignmentCenter. Here's an image of what it looks like:

As you can see.. I have no right UIBarButtonItem in use, but this shouldn't be keeping the text from being centered? Here's the code, in the viewDidLoad:

//Set the title of navBar----------

UILabel  *navTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];  //0 0 320 10

navTitleLabel.backgroundColor = [UIColor clearColor];
navTitleLabel.textAlignment = NSTextAlignmentCenter;
navTitleLabel.textColor=[UIColor whiteColor];
navTitleLabel.text = @"IMG";
[navTitleLabel setFont: [UIFont fontWithName:@"BullettoKilla" size:16]];

self.navigationItem.titleView = navTitleLabel;

What am I doing wrong?

Chisx
  • 1,976
  • 4
  • 25
  • 53

2 Answers2

0

As pointed out by 0yeoj you have set the item width to 320. Try something smaller.

Community
  • 1
  • 1
Ric Santos
  • 15,419
  • 6
  • 50
  • 75
-1

Try this UILabel *navTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 40)];

Saheb Roy
  • 5,899
  • 3
  • 23
  • 35