美文网首页导航栏和底部栏
关于系统导航栏背景在iOS 15中自动隐藏的问题

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

作者: 健了个平_24 | 来源:发表于2021-09-25 16:31 被阅读0次

    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.

    相关文章

      网友评论

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

        本文链接:https://www.haomeiwen.com/subject/cjmxnltx.html