iPhone plus and iPad UIBarMetrics (different landscape/portrait images for navigation bar)

487 views Asked by At

How to set 2 different (red and green for example) images for navigation bar background for landscape and portrait orientation on iPhone (6-6s-7) plus or iPad?

[[UINavigationBar appearance] setBackgroundImage:redImage
                                   forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:greenImage
                                   forBarMetrics:UIBarMetricsCompact];

Don't work for iPad or iPhone * plus, just always used redImage for both orientations. But works with all other iPhone (and even iPod i guess).

Additionally, will be great to read something about difference in bar metrics (and anything else) between iPhone and iPad / iPhone plus, cause it (nav bar background image) isn't only one thing with specific behavior on (iPhone plus, iPad) that i have realized (different separator inset's in table view, etc.).

Thank, you.

1

There are 1 answers

0
Andrew Romanov On BEST ANSWER

You can subscribe your view controller class (or other class that can control status bar) to UIApplicationDidChangeStatusBarOrientationNotification. In the handler, you should update your background image:

<...>
if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait ||
            [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        image = redImage;
    }
    else
    {
        image = greenImage;
    }
<...>

You can't use UIBarMetrics for this, because your task uses interface orientation as criterion, but UIBarMetrics bases on size classes. Therefore you can't use appearance to achieve required behaviour.

I recommend for you create custom subclass of UINavigationBar and implement that logic inside it.