9
- (void)viewDidLoad {
   [super viewDidLoad];
   [self.navigationController.navigationBar setBackgroundImage:xxx] forBarMetrics:UIBarMetricsDefault];
   [self.navigationController.navigationBar setBarTintColor:[UIColor redColor]];
}

it works perfect on iOS14. but on iOS15, XCode13 beta, it doesn't work anymore.

4 Answers4

12

OC:

if (@available(iOS 15.0, *)) {
    UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
    [appearance configureWithOpaqueBackground];
    appearance.backgroundColor = [UIColor whiteColor];
    appearance.shadowColor = [UIColor whiteColor];
    appearance.shadowImage = [UIImage imageWithColor:[UIColor whiteColor]];
    self.navigationController.navigationBar.standardAppearance = appearance;
    self.navigationController.navigationBar.scrollEdgeAppearance = self.navigationController.navigationBar.standardAppearance;
}
    

Swift:

if #available(iOS 15.0, *) {
    let appearance = UINavigationBarAppearance()
    appearance.configureWithOpaqueBackground()
    appearance.backgroundColor = .white
    appearance.shadowColor = .white
    appearance.shadowImage = UIImage.color(.white)
    navigationController?.navigationBar.standardAppearance = appearance
    navigationController?.navigationBar.scrollEdgeAppearance = navigationController?.navigationBar.standardAppearance
}
José
  • 3,112
  • 1
  • 29
  • 42
user16286722
  • 121
  • 3
5

swift:

if #available(iOS 15.0, *) {
        let appearance = UINavigationBarAppearance()
        appearance.configureWithTransparentBackground()
        appearance.backgroundImage = image
        navigationController?.navigationBar.standardAppearance = appearance
        navigationController?.navigationBar.scrollEdgeAppearance = navigationController?.navigationBar.standardAppearance
    }else{
        self.navigationController?.navigationBar.setBackgroundImage(image, for: .default)
    }
NEO
  • 171
  • 3
  • 7
4

just use this code, the background image can work in iOS 15

if (@available(iOS 13.0, *)) {
  UINavigationBarAppearance *navigationBarAppearance = [UINavigationBarAppearance new];
  [navigationBarAppearance configureWithOpaqueBackground];
  [navigationBarAppearance setBackgroundImage:image];
  self.navigationController.navigationBar.scrollEdgeAppearance = navigationBarAppearance;
  self.navigationController.navigationBar.standardAppearance = navigationBarAppearance;
}
Xinboy
  • 71
  • 6
  • As for iOS 15.0 --- Setting the background image on the navigationBarAppearance object actually did the trick. It took me an unreasonable amount of time to figure that out. So instead of using setBackgroundImage on the UINavigationBar object, one should do it on the UINavigationBarAppearance object. The former hasn't had any effect on my navigation bar. – Oliver Schobel Oct 11 '21 at 12:30
0

I has the same issue and here is my code:

if (@available(iOS 15.0, *)) {
        UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
        [appearance configureWithOpaqueBackground];
        //appearance.backgroundColor = [UIColor blueColor];
        appearance.shadowColor = [UIColor whiteColor];
        appearance.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:24.0/255.0 green:44.0/255.0 blue:122.0/255.0 alpha:1.0f], NSForegroundColorAttributeName,[UIFont fontWithName:@"AvenirNext-Demibold" size:21], NSFontAttributeName,nil];
        appearance.backgroundImage = [UIImage imageNamed:@"Bg_portrait_540x120_1.png"];
        self.navigationController.navigationBar.standardAppearance = appearance;
        self.navigationController.navigationBar.scrollEdgeAppearance = self.navigationController.navigationBar.standardAppearance;
    }
Panayot
  • 484
  • 1
  • 6
  • 18