Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

navigationbar setBackgroundImage not working on iOS15

Tags:

ios15

- (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.

like image 413
highmore highmore Avatar asked Jun 20 '21 14:06

highmore highmore


People also ask

How do I change the navigation bar on my Iphone?

Change the Bar Style A user changes the navigation bar's style, or UIBarStyle , by tapping the “Style” button to the left of the main page. This button opens an action sheet where users can change the background's appearance to default, black-opaque, or black- translucent.

How do I use the navigation bar in Xcode?

To add a navigation bar to your interface, the following steps are required: Set up Auto Layout rules to govern the position of the navigation bar in your interface. Create a root navigation item to supply the initial title. Configure a delegate object to handle user interactions with the navigation bar.


Video Answer


3 Answers

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
}
like image 69
user16286722 Avatar answered Oct 28 '22 13:10

user16286722


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;
}
like image 42
Xinboy Avatar answered Oct 28 '22 11:10

Xinboy


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)
    }
like image 34
NEO Avatar answered Oct 28 '22 12:10

NEO