iOS 15系统导航栏适配

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

iOS 15的系统导航栏背景默认静止时隐藏,得页面能滑动且有内容经过导航栏区域才会显示...

iOS15默认样式.GIF

解决方法

iOS 15后,需要手动设置UINavigationBarscrollEdgeAppearancestandardAppearance属性才行。

// OC
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    if (@available(iOS 15.0, *)) {
        UINavigationBar *navigationBar = [UINavigationBar appearance];
        
        UINavigationBarAppearance *scrollEdgeAppearance = [[UINavigationBarAppearance alloc] init];
        scrollEdgeAppearance.backgroundColor = UIColor.redColor;
        navigationBar.scrollEdgeAppearance = scrollEdgeAppearance;
        
        UINavigationBarAppearance *standardAppearance = [[UINavigationBarAppearance alloc] init];
        standardAppearance.backgroundColor = UIColor.greenColor;
        navigationBar.standardAppearance = standardAppearance;
    }
    
    return YES;
}
// Swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    if #available(iOS 15.0, *) {
        let navigationBar = UINavigationBar.appearance()

        navigationBar.scrollEdgeAppearance = {
            let appearance = UINavigationBarAppearance()
            appearance.backgroundColor = .red
            return appearance
        }()

        navigationBar.standardAppearance = {
            let appearance = UINavigationBarAppearance()
            appearance.backgroundColor = .green
            return appearance
        }()
    }
    
    return true
}

设置后的样式.GIF

从效果上看的出:

  • scrollEdgeAppearance:是处于顶部时的背景
  • standardAppearance:是滑动后的背景

更多的自定义效果都可以在对应的UINavigationBarAppearance实例里面设置其属性。

如果想统一样式,scrollEdgeAppearancestandardAppearance都设置同一个appearance即可(不设置任何属性则是默认的毛玻璃效果):

// OC
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    if (@available(iOS 15.0, *)) {
        UINavigationBar *navigationBar = [UINavigationBar appearance];
        
        UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
        navigationBar.scrollEdgeAppearance = appearance;
        navigationBar.standardAppearance = appearance;
    }
    
    return YES;
}
// Swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    if #available(iOS 15.0, *) {
        let navigationBar = UINavigationBar.appearance()

        let appearance = UINavigationBarAppearance()
        navigationBar.scrollEdgeAppearance = appearance
        navigationBar.standardAppearance = appearance
    }

    return true
}

以前的样式.GIF

原文地址:https://www.cnblogs.com/strengthen/p/15702105.html