Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Misaligned title in UINavigationBar since iOS6

Ever since iOS 6 I have several problems with using custom styling in my application. I use a custom font and several UIAppearance proxies. A problem I can't get my head around is the misalignment of the title in my UINavigationBar. In iOS 5 everything worked fine and was correctly aligned.

Since iOS6 has been released and custom styling is not uncommon I assume this isn't a bug but my misunderstanding of some new change to iOS6.

misalignment

I've searched the documentation for a text alignment method to call on the UIAppearance proxy, but I was unable to find such a method.

I use the following lines of code to style my UINavigationBar across my entire application:

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"NavigationBarBackground"] 
                                   forBarMetrics:UIBarMetricsDefault];

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"NavigationBarBackground"] 
                                   forBarMetrics:UIBarMetricsLandscapePhone];

[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                      [UIColor whiteColor], UITextAttributeTextColor, 
                                                      [UIFont fontWithName:@"Corbel-Bold" size:14.0], UITextAttributeFont,
                                                      nil]];


[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTintColor:[UIColor whiteColor]];
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                      [UIColor ceBlueColor], UITextAttributeTextColor,
                                                      [UIColor whiteColor], UITextAttributeTextShadowColor, 
                                                      [NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset, 
                                                      [UIFont fontWithName:@"Corbel" size:0.0], UITextAttributeFont, 
                                                      nil]
                                            forState:UIControlStateNormal];

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -3) forBarMetrics:UIBarMetricsDefault];
like image 511
thijsai Avatar asked Dec 03 '22 01:12

thijsai


1 Answers

Same issue here, but I noticed it doesn't happen when the navigation bar has either a back button or a right bar button (table edit). The alignment on the title is also fixed when navigating to a new view controller and then going back to the first one...

What I think that is happening is that iOS calculates the position of the frame of the title based on the default font, because the font I use is a bit smaller the title misaligns a bit to the left of the center.

My current fix is calling setNeedsLayout in viewWillAppear. Seems to be working.

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    if (self.navigationItem.hidesBackButton || self.navigationItem.rightBarButtonItem == nil) {
        [self.navigationController.navigationBar setNeedsLayout];
    }
}
like image 76
Dries_S Avatar answered Dec 19 '22 07:12

Dries_S