2
votes

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
here developer.apple.com/reference/uikit/uitraitcollection you can read about size classes. And find that iPad has same size classes for both dimensions. You should use UIInterfaceOrientation and write custom _isIPad method to select correct images for different orientations.Andrew Romanov
Please take a look at apple documentation for size classes: developer.apple.com/library/content/documentation/… I hope this will be helpful for you.dvp.petrov
Thank you guys, your comments are helpful.SiavA

1 Answers

1
votes

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.