美文网首页
SwiftUI NavigationView设置导航条颜色

SwiftUI NavigationView设置导航条颜色

作者: 笨驴爱吃胡萝卜 | 来源:发表于2020-09-28 14:41 被阅读0次

    SwiftUI NavigationView设置导航条颜色

        var body: some View {
    
            NavigationView {
                    List {
                        }
                  .navigationBarUIColor(ColorManager.mainColorBlue)
    
        }
    extension View {
        
        func navigationBarUIColor(_ backgroundColor: Color?) -> some View {
            self.modifier(NavigationBarModifier(backgroundColor: UIColor(backgroundColor!)))
        }
    
    }
    
    
    
    

    新建一个文件

    
    
    //
    //  NavigationBarModifier.swift
    //  SwiftUI2
    //
    //  Created by TongBin Mac on 2020/9/28.
    //
    
    import SwiftUI
    
    struct NavigationBarModifier: ViewModifier {
            
        var backgroundColor: UIColor?
        var backColor: Color?
        
        init( backgroundColor: UIColor?) {
            self.backgroundColor = backgroundColor
            let coloredAppearance = UINavigationBarAppearance()
            coloredAppearance.configureWithTransparentBackground()
            coloredAppearance.backgroundColor = .clear
            coloredAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
            coloredAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
            
            UINavigationBar.appearance().standardAppearance = coloredAppearance
            UINavigationBar.appearance().compactAppearance = coloredAppearance
            UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance
            UINavigationBar.appearance().tintColor = .white
    
        }
        
        func body(content: Content) -> some View {
            ZStack{
                content
                VStack {
                    GeometryReader { geometry in
                        Color(self.backgroundColor ?? .clear)
                            .frame(height: geometry.safeAreaInsets.top)
                            .edgesIgnoringSafeArea(.top)
                        Spacer()
                    }
                }
            }
        }
    }
    
    
    

    optinon + commond + P 看一下效果


    image.png

    ps 对于网上这个功能也收费,表示不明白为什么

    相关文章

      网友评论

          本文标题:SwiftUI NavigationView设置导航条颜色

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