Nick Kirsten
Nick Kirsten

Reputation: 1187

Navigation bar resizes when app becomes active

I have an issue where I have a custom navigation bar where I have changed the height to a custom value. The issue I am having is when I exit the app, then go back into the app (becomes active again) the navigation bar shifts and resets it's height.

I've tried running the same code I use to change the navigation bar height when the app becomes active but this does not help at all. The code is:

[self.navigationBar setBounds:[self getBarHeightAdjustment]];

- (CGRect) getBarHeightAdjustment {
    CGFloat height= 80;
    if ([UIApplication isPhone]) {
        height = 45;
    }
    return CGRectMake(self.navigationBar.bounds.origin.x, self.navigationBar.bounds.origin.y, self.navigationBar.bounds.size.width, height);
}

I have 2 screenshots to illustrate my problem.

How the nav bar looks when app is launched: http://cl.ly/image/473B0v0h143t

How the nav bar looks after pressing home button then going back in: http://cl.ly/image/2n012k190r12

Any help would be greatly appreciated!

Upvotes: 1

Views: 177

Answers (1)

Nick Kirsten
Nick Kirsten

Reputation: 1187

The only way I could fix this (and i'm not too proud of the fix to be honest but could not fix it any other way) was to fire a notification when the application becomes active, then reset the ui appearance after a delay of 0.1 (yes calling the method directly did not work).

The code is:

- (void) setupAppearanceDelayed {
    [self performSelector:@selector(configureForAssetNavigation) withObject:nil afterDelay:0.1];
    [self performSelector:@selector(setupAppearance) withObject:nil afterDelay:0.1];
}

- (void) setupAppearance {
    [[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x2a2a2a)];
    [[UINavigationBar appearance] setTintColor:kColours_navBarLabelColour];
    [[UINavigationBar appearance] setTitleTextAttributes:[self getTitleTextAttributes]];

    if([UIApplication isPad]) {
        [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:-4 forBarMetrics:UIBarMetricsDefault];
    }
    else {
        [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:+3 forBarMetrics:UIBarMetricsDefault];
    }

    self.navigationBar.tintColor =UIColorFromRGB(0x2a2a2a);
    self.navigationBar.translucent = NO;
}

If anyone has a better solution I would love to hear it as this seems like a bit of a hack

Upvotes: 2

Related Questions