23

I have a custom made UINavigationBar (different size, background etc) that has inside a custom title view. I used this code to achieve this:

UIView * mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    mainView.backgroundColor = [UIColor yellowColor];

    UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0,kScreenWidth,80)];
    navBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;

    UINavigationItem *currentItem = [[UINavigationItem alloc] init];
    UILabel *label = [[UILabel alloc] init];
    label.font = [UIFont fontWithName:@"Helvetica-Bold" size: 30.0];
    [label setBackgroundColor:[UIColor clearColor]];
    [label setTextColor:[UIColor whiteColor]];
    [label setText:self.title];
    [label sizeToFit];
    [currentItem setTitleView:label];
    [label release];

    [navBar pushNavigationItem:currentItem animated:NO];
    [currentItem release];

    [mainView addSubview:navBar];
    [navBar release];    

    self.view = mainView;
[mainView release];

However, my problem now is that, even if the custom title view is correctly added, it's not vertically centered with the NavBar. What I am missing?

Thanks!

Rad'Val
  • 8,895
  • 9
  • 62
  • 92
  • 7
    For the love of… **do not** hard code screen sizes, Apple has evangelized about this since the iPhone SDK was first released. Use `[UIScreen mainScreen].applicationFrame`, and it will work on iPhone/iPad, or any future device. With and without the bigger status bar when in a call, or tethering. – PeyloW May 13 '11 at 14:55
  • Thanks PeyloW, will keep that in mind from now on! – Rad'Val May 13 '11 at 14:59
  • Don't use UIScreen's frame it will give you the wrong size when your app is in split screen mode on iOS 9. Use the window's frame instead. – Steve Moser Aug 12 '15 at 17:51

2 Answers2

97

You can use setTitleVerticalPositionAdjustment:forBarMetrics: in iOS 5 to change the title position, it works on normal title e.g.:

CGFloat verticalOffset = -4;
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:verticalOffset forBarMetrics:UIBarMetricsDefault];
Cœur
  • 37,241
  • 25
  • 195
  • 267
Tobias Ahlin
  • 982
  • 7
  • 5
  • Unfortunately sometimes one needs to offset a title horizontally from the center to look right. For example in a UINavigationBar, you might have a button that says "Cancel" on the left, and "Done" on the right. Even though the title is centered in the bar, it is not centered between the buttons and doesn't look right. So working with the UINavigationBarItem.titleView might be the way to go in some cases. – clearlight Feb 01 '15 at 22:25
0

You created you UINavigationBar with a height of 80 points, when a standard UINavigationBar has 44 pts. It probably position it centred it on a 44 pts based centre...

You can try to make a UIView with a height of 80 pts and add the UILabel centred inside it. (not tested, just a guess)

gcamp
  • 14,622
  • 4
  • 54
  • 85
  • I had the same feeling, but I made the `UINavigationBar` even larger and still the Title kept its position (several pts above the bottom line of the `UINavigationBar`). Basically, no matter how big the `UINavigationBar` is, the title will be several pts above its bottom line. Will try the UIView thing right away and report back. – Rad'Val May 13 '11 at 14:20
  • adding a larger UIView works (if I set it the same size as the `UINavigationBar`) Thanks. – Rad'Val May 13 '11 at 20:02