Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UINavigationBar title gets clipped while changing viewControllers

When I push a new tableViewController from the starting screen of the iOS app (I push the Settings screen) the title in the UINavigationController gets clipped until the animation finishes:

enter image description here

That is the NavigationBar in mid animation, and just before the animation finishes, it looks like this:

enter image description here

After a moment, the title changes correctly to "Settings". It's not a big deal, but you can imagine how much it bothers a slightly OCD-prone programmer! :)

Here's the code in the tableViewController where I set the title, nothing special:

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        self.title = @"Settings";
        // Hide tabBar when pushed so you cannot switch from the Settings
        self.hidesBottomBarWhenPushed = YES;
        self.tableView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"bg.png"]];
    }
    return self;
}
like image 580
nikolovski Avatar asked Mar 13 '12 16:03

nikolovski


1 Answers

I'm a bit late with the answer, but I tracked down the issue on iOS 5.. When you use the UIAppearance proxy on UINavigationBar, it appears you need to explicitly set the font size, instead of using 0.0 to let it auto set based on orientation.

I was able to fix this by subclassing UINavigationController and putting in the following code:

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];

    // You should include a conditional here to check for iOS 5, so iOS 6 doesn't have to do any additional work
    self.navigationBar.titleTextAttributes = @{
        UITextAttributeFont:[UIFont boldSystemFontOfSize:UIInterfaceOrientationIsPortrait(self.interfaceOrientation) || IS_IPAD ? 20.0f : 16.0f],
        UITextAttributeTextColor:[UIColor whiteColor],
        UITextAttributeTextShadowColor:[UIColor colorWithWhite:0.0f alpha:0.5f],
        UITextAttributeTextShadowOffset:[NSValue valueWithUIOffset:UIOffsetMake(0.0f, -1.0f)]
    };
}
like image 163
Shaun Avatar answered Oct 21 '22 04:10

Shaun