iOS15 导航器适配记录

2021-09-28  本文已影响0人  整个夏天

iOS15系统下强制了对navigationBar统一的样式管理,在应用打开的时候需要进行样式的默认设置,这样统一设置后每个导航器的背景、分割线都会变成统一的样式,如下。

if (@available(iOS 15.0, *)) {
        UINavigationBarAppearance *app = [UINavigationBarAppearance new];
        [app configureWithDefaultBackground];
        
        app.backgroundColor = UIColor.whiteColor;
        app.backgroundImage = [UIImage bt_imageWithColor:UIColor.redColor equalSize:100];
        app.shadowColor = UIColor.yellowColor;
        
        [UINavigationBar appearance].scrollEdgeAppearance = app;
        [UINavigationBar appearance].standardAppearance = app;
    }

这里的standardAppearance为其它Appearance属性的默认值,不设置的话在有UIScrollView的界面进行滑动的时候导航器样式会改变。其它Appearance源码如下:

///  When set and this item is topmost, overrides the hosting navigation bar's standardAppearance. See UINavigationBar.standardAppearance for further details.
@property (nonatomic, readwrite, copy, nullable) UINavigationBarAppearance *standardAppearance API_AVAILABLE(ios(13.0), tvos(13.0));
///  When set and this item is topmost, overrides the hosting navigation bar's compactAppearance. See UINavigationBar.compactAppearance for further details.
@property (nonatomic, readwrite, copy, nullable) UINavigationBarAppearance *compactAppearance API_AVAILABLE(ios(13.0));
///  When set and this item is topmost, overrides the hosting navigation bar's scrollEdgeAppearance. See UINavigationBar.scrollEdgeAppearance for further details.
@property (nonatomic, readwrite, copy, nullable) UINavigationBarAppearance *scrollEdgeAppearance API_AVAILABLE(ios(13.0));
///  When set and this item is topmost, overrides the hosting navigation bar's compactScrollEdgeAppearance. See UINavigationBar.h for further details.
@property (nonatomic, readwrite, copy, nullable) UINavigationBarAppearance *compactScrollEdgeAppearance API_AVAILABLE(ios(15.0));

导航器个别样式的设置方法总结

这个时候如果有一个VC需要将分割线隐藏,或者设置分割线的颜色用以前的方法会失效,这里总结一些导航器个别样式常用的设置方法。

隐藏分割线

if (@available(iOS 15.0, *)) {
        UINavigationBarAppearance * app = self.navigationItem.standardAppearance;
        if (app == nil) {
            app = [UINavigationBar.appearance.standardAppearance copy];
        }
        app.shadowColor = UIColor.clearColor;
        self.navigationItem.scrollEdgeAppearance = app;
        self.navigationItem.standardAppearance = app;
        return;
    }
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];

设置分割线颜色,iOS15分割线高度不能设置

 if (@available(iOS 15.0, *)) {
        UINavigationBarAppearance * app = self.navigationItem.standardAppearance;
        if (app == nil) {
            app = [UINavigationBar.appearance.standardAppearance copy];
        }
        app.shadowColor = color;
        self.navigationItem.scrollEdgeAppearance = app;
        self.navigationItem.standardAppearance = app;
        return;
    }
    [self.navigationController.navigationBar setShadowImage:[UIImage bt_imageWithColor:color size:CGSizeMake(BTUtils.SCREEN_W, height)]];

设置导航器背景图片

if (@available(iOS 15.0, *)) {
        UINavigationBarAppearance * app = self.navigationItem.standardAppearance;
        if (app == nil) {
            app = [UINavigationBar.appearance.standardAppearance copy];
        }
        [app configureWithDefaultBackground];
        app.backgroundImage = bgImg;
        self.navigationItem.scrollEdgeAppearance = app;
        self.navigationItem.standardAppearance = app;
        return;
    }
    
    
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationBar setBackgroundImage:bgImg forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    [self.navigationController.navigationBar setClipsToBounds:NO];

设置导航器背景颜色

   if (@available(iOS 15.0, *)) {
        UINavigationBarAppearance * app = self.navigationItem.standardAppearance;
        if (app == nil) {
            app = [UINavigationBar.appearance.standardAppearance copy];
        }
        [app configureWithDefaultBackground];
        app.backgroundImage = [UIImage bt_imageWithColor:color size:CGSizeMake(BTUtils.SCREEN_W, BTUtils.NAV_HEIGHT)];
        app.backgroundColor = color;
        self.navigationItem.scrollEdgeAppearance = app;
        self.navigationItem.standardAppearance = app;
        return;
    }
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage bt_imageWithColor:color] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    [self.navigationController.navigationBar setClipsToBounds:NO];
    self.navigationController.navigationBar.backgroundColor=color;

设置导航器透明

    if (@available(iOS 15.0, *)) {
        UINavigationBarAppearance * app = self.navigationItem.standardAppearance;
        if (app == nil) {
            app = [UINavigationBar.appearance.standardAppearance copy];
        }
        [app configureWithTransparentBackground];
        app.backgroundImage = [UIImage bt_imageWithColor:UIColor.clearColor size:CGSizeMake(BTUtils.SCREEN_W, BTUtils.NAV_HEIGHT)];
        app.backgroundColor = UIColor.clearColor;
        self.navigationItem.scrollEdgeAppearance = app;
        self.navigationItem.standardAppearance = app;
        self.navigationController.navigationBar.translucent = YES;
        return;
    }
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage bt_imageWithColor:UIColor.clearColor] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    [self.navigationController.navigationBar setClipsToBounds:YES];
    self.navigationController.navigationBar.backgroundColor=UIColor.clearColor;
上一篇下一篇

猜你喜欢

热点阅读