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

它在 iOS14 上完美运行。但在 iOS15、XCode13 beta 上,它不再工作了。

4

4 回答 4

5

超频:

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;
}
    

迅速:

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
}
于 2021-06-22T05:47:32.653 回答
3

迅速:

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)
    }
于 2021-09-30T08:55:25.843 回答
3

只需使用此代码,背景图像就可以在 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;
}
于 2021-09-23T07:11:04.660 回答
0

我有同样的问题,这是我的代码:

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;
    }
于 2021-12-15T19:56:09.407 回答