关于系统导航栏背景在iOS 15中自动隐藏的问题

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

That's all, thanks.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容