Navigation controller background image repeat it self in ios 11 using xcode 9

Viewed 469

I had tried following code to set background image of UINavigationBar. It was working fine before Xcode 9 but in Xcode 9 image are not set properly.

UIImage *image = [UIImage imageNamed:@"headr_bg"];
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];

Xcode 9 image 1

Xcode 9 image 2

[before Xcode 9]

2 Answers

may be its not perfect solution & but i had old project in that i had fixed it by below code. Just updated navigation bar position and navigation view frame for ios version > 11.

CGRect navbarFrame = self.navigationController.navigationBar.frame;
CGRect navFrame = self.navigationController.view.frame;
navbarFrame.size.height = 44;
navFrame.origin.y = 20;
self.navigationController.navigationBar.frame = navbarFrame;
self.navigationController.view.frame = navFrame;

I had the same issue however I could not find a fix working with insets or with a layout based approach. Ultimately a quick work around was to create a secondary background image and pad the top of the original background with 20px with the previous color of your status bar (black in your case).

Then I simply added code to change the image depending if this is iOS 11+ like so:

NSString* navBarImg = @"navbar.png";

if (@available(iOS 11.0, *)) {
    navBarImg = @"navbar_ios11.png";
}

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:navBarImg] forBarMetrics:UIBarMetricsDefault];

This may not work or may need tweaks depending if the status bar visibility is shown/hidden at some points. Hopefully this will help until a better approach is discovered.

Related