Question

Despite globally altering your UINavigationBar title through the appearance proxy such as this:

[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:0.5 forBarMetrics:UIBarMetricsDefault];

How can you define the title's vertical height for a single view controller which I would expect to look like this:

[self.navigationBar setTitleVerticalPositionAdjustment:0.5 forBarMetrics:UIBarMetricsDefault];

But unfortunately does not work. I am trying to adjust my titles' vertical position for a specific view controller without affecting the other view controllers' UINavigationBar title position.

Était-ce utile?

La solution

You could use the appearanceWhenContainedIn: selector to set the appearance for each individual UIViewController. It would look something like this...

[[UINavigationBar appearanceWhenContainedIn:[ViewControllerOne class], nil] setTitleVerticalPositionAdjustment:0.3 forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearanceWhenContainedIn:[ViewControllerTwo class], nil] setTitleVerticalPositionAdjustment:0.5 forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearanceWhenContainedIn:[ViewControllerThree class], nil] setTitleVerticalPositionAdjustment:0.7 forBarMetrics:UIBarMetricsDefault];

Update:

Example of how to use this by creating a StyleManager class:

@implementation StyleManager

+ (void)setApplicationStyle
{
    [[UINavigationBar appearanceWhenContainedIn:[ViewControllerOne class], nil] setTitleVerticalPositionAdjustment:0.3 forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearanceWhenContainedIn:[ViewControllerTwo class], nil] setTitleVerticalPositionAdjustment:0.5 forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearanceWhenContainedIn:[ViewControllerThree class], nil] setTitleVerticalPositionAdjustment:0.7 forBarMetrics:UIBarMetricsDefault];

    // Other appearnce styling here...
}

@end

Once you've created your StyleManager you can simply use it in your AppDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [StyleManager setApplicationStyle];
}

Let me know if this works for you.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top