SwiftUI|SwiftUI NavigationView设置导航条颜色

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 SwiftUIstruct 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() } } } } }

【SwiftUI|SwiftUI NavigationView设置导航条颜色】optinon + commond + P 看一下效果

SwiftUI|SwiftUI NavigationView设置导航条颜色
文章图片
image.png ps 对于网上这个功能也收费,表示不明白为什么

    推荐阅读