4

The following code was working perfectly fine for setting a custom image as the background of a UINavigationBar:

    // In AppDelegate.swift: 
    let image = UIImage(named:"HeaderBanner-new")
    UINavigationBar.appearance().setBackgroundImage(image, for: UIBarMetrics.defaultPrompt)

Since switching to Xcode 9, Swift 4, and iOS 11, this code no longer works. All I'm getting is a plain white background in the UINavigationBar.

I also tried moving the code out of the AppDelegate.swift and putting it directly in my root custom UINavgiationController.swift file:

    let image = UIImage(named:"HeaderBanner-new")
    self.navigationBar.setBackgroundImage(image, for: UIBarMetrics.defaultPrompt)

Still doesn't work. Any ideas what's going on - or ideas for a workaround/hack?

Sirab33
  • 1,247
  • 3
  • 12
  • 27
  • My code is older so not in Swift, but my ObjC actually is the same but uses a resizable image and it does set the image, but does not resize it vertically (it does horizontally). – chadbag Nov 15 '17 at 20:48
  • UIImage *bgi = [UIImage imageNamed:@"nav-bar"]; UIImage *bgir = [bgi resizableImageWithCapInsets:UIEdgeInsetsZero]; [[UINavigationBar appearance] setBackgroundImage:bgir forBarMetrics:UIBarMetricsDefault]; – chadbag Nov 15 '17 at 20:49

1 Answers1

1

It seems NavigationBar.setBackgroundImage doesn't work as expected in iOS 11.

I had the same issue and fixed it by using NavigationBar.barTintColor and it worked.

Refer this answer.

Bin0li
  • 168
  • 16