美文网首页
iOS15导航栏适配

iOS15导航栏适配

作者: 小猪蛋蛋 | 来源:发表于2021-11-17 13:14 被阅读0次

    苹果之前在iOS 13中为UINavigationBar增加了scrollEdgeAppearance属性,用在iOS 14及更早版本的大标题导航栏上,但是在iOS 15中scrollEdgeAppearance属性适用于所有的导航栏。
    配置导航栏颜色、分割线、阴影的常用属性涉及到:

    backgroundColor -背景色,位于backgroundImage之下
    backgroundImage -背景图片
    backgroundEffect -基于backgroundColor或backgroundImage的磨砂效果
    backgroundImageContentMode -渲染backgroundImage时的内容模式。 默认为UIViewContentModeScaleToFill
    shadowColor -阴影颜色  当shadowImage为nil时,使用此颜色为阴影色。如果此属性为nil或clearColor,则不显示阴影。
    shadowImage -阴影图片(可以使用图片的渲染模式:UIImageRenderingModeAlwaysOriginal 保持原色)
    

    导航栏设置

    /// 导航栏设置
        /// - Parameters:
        ///   - titleColor: 标题颜色
        ///   - tintColor: 按钮颜色
        ///   - barTintColor: 背景色
        ///   - lineIshidden: 隐藏横线
        ///   - isLucency: 导航栏透明
        func customNavigationBar( titleColor: UIColor = MColor.rgba(51, 51, 51, 1), tintColor: UIColor = MColor.rgba(51, 51, 51, 1), barTintColor: UIColor = MColor.rgba(255, 255, 255, 1), lineIshidden: Bool = false, isLucency: Bool = false){
            
            //兼容iOS15设置导航栏
            if #available(iOS 13.0, *) {
                let barApp = UINavigationBarAppearance()
    //            barApp.configureWithTransparentBackground() //配置具有透明背景且无阴影的条形外观对象。
    //            barApp.configureWithDefaultBackground() //使用默认背景和阴影值配置条形外观对象。
    //            barApp.configureWithOpaqueBackground() //使用一组适合当前主题的不透明颜色配置栏外观对象。
                barApp.backgroundColor = isLucency ? UIColor.clear : MColor.rgba(255, 255, 255, 1)
                //基于backgroundColor或backgroundImage的磨砂效果
                barApp.backgroundEffect = nil
                //阴影颜色
                barApp.shadowColor = lineIshidden ? UIColor.clear : MColor.rgba(248, 248, 253, 1)
                //标题文本的字符串属性
                barApp.titleTextAttributes = [.foregroundColor: titleColor]
                navigationController?.navigationBar.scrollEdgeAppearance = barApp
                navigationController?.navigationBar.standardAppearance = barApp
            }else{
                //设置标题
                navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: titleColor]
                //隐藏导航栏横线
                navigationController?.navigationBar.shadowImage = lineIshidden ? UIImage() : nil
                //导航栏透明
                navigationController?.navigationBar.setBackgroundImage(isLucency ? UIImage() : nil, for: .default)
            }
            self.navigationController?.navigationBar.tintColor = tintColor
    //        self.navigationController?.navigationBar.barTintColor = barTintColor
        }
    

    相关文章

      网友评论

          本文标题:iOS15导航栏适配

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