【发布时间】:2019-06-26 13:04:44
【问题描述】:
目前我找不到任何方法来更改 SwiftUI 中导航栏的颜色/背景。有什么建议吗?
【问题讨论】:
标签: navigationbar swiftui
目前我找不到任何方法来更改 SwiftUI 中导航栏的颜色/背景。有什么建议吗?
【问题讨论】:
标签: navigationbar swiftui
为了改变所有视图控制器的导航栏颜色,你必须在AppDelegate.swift文件中设置它
在AppDelegate.swift中的didFinishLaunchingWithOptions函数中添加以下代码
var navigationBarAppearace = UINavigationBar.appearance()
navigationBarAppearace.tintColor = uicolorFromHex(0xffffff)
navigationBarAppearace.barTintColor = uicolorFromHex(0x034517)
在这里tintColor属性改变导航栏的背景颜色。
barTintColor 属性对颜色的影响:
奖励:
改变导航栏标题的颜色:
// change navigation item title color
navigationBarAppearace.titleTextAttributes =[NSForegroundColorAttributeName:UIColor.whiteColor()]
titleTextAttributes 影响标题文本
我希望它有所帮助。 :)
【讨论】:
在SwiftUI中,此时我们不能直接改变它,但是你可以像这样改变navigationBar的外观,
struct YourView: View {
init() {
UINavigationBar.appearance().backgroundColor = .orange
//Use this if NavigationBarTitle is with Large Font
UINavigationBar.appearance().largeTitleTextAttributes = [.font : UIFont(name: "Georgia-Bold", size: 20)!]
//Use this if NavigationBarTitle is with displayMode = .inline
//UINavigationBar.appearance().titleTextAttributes = [.font : UIFont(name: "Georgia-Bold", size: 20)!]
}
var body: some View {
NavigationView {
Text("Hello World!")
.navigationBarTitle(Text("Dashboard").font(.subheadline), displayMode: .large)
//.navigationBarTitle (Text("Dashboard"), displayMode: .inline)
}
}
}
我希望这会对你有所帮助。谢谢!!
【讨论】:
到目前为止,SwiftUI 中还没有用于此目的的明确 API。但是您可以使用外观 API。这是一个示例代码。
import SwiftUI
struct ContentView : View {
init() {
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.red]
UINavigationBar.appearance().backgroundColor = .green
}
var body: some View {
NavigationView {
NavigationButton(destination: SecondPage(), label: {
Text("Click")
})
.navigationBarTitle(Text("Title"), displayMode: .inline)
}
}
}
【讨论】:
在NavigationView 后面加上Rectangle ZStack:
ZStack {
Rectangle().foregroundColor(.red)
NavigationView {
...
}
}
【讨论】:
有关不使用.appearance() 的解决方案,请参阅this answer。
简称UIViewControllerRepresentable
func updateUIViewController(_ uiViewController: UIViewController, context: UIViewControllerRepresentableContext<NavigationConfigurator>) {
uiViewController.navigationController?.navigationBar...
}
【讨论】:
使用Introspect,您可以这样做:
NavigationView {
Text("Item 2")
.introspectNavigationController { navigationController in
navigationController.navigationBar.backgroundColor = .red
}
}
【讨论】:
init() 或onAppear() 上使用UINavigationBar.appearance(),它不仅会影响您当前的视图,而且您的解决方案只会影响视图本身,谢谢
有一点我一开始不明白:SwiftUI 会根据你是否处于夜间模式来改变 NavigationBar 之类的东西的外观。
如果您想将其默认为不同的配色方案,请添加
.colorScheme(.dark)
如果您使用本文所述的颜色集系统创建配色方案:https://www.freecodecamp.org/news/how-to-build-design-system-with-swiftui/,它将应用于导航和标签栏等主要元素,并允许您为夜间/白天模式应用不同的方案。
【讨论】:
NavigationView 正在管理全屏内容。这些屏幕中的每一个都有自己的背景颜色。因此,您可以使用以下方法将背景颜色应用到屏幕上:
let backgroundColor = Color(red: 0.8, green: 0.9, blue: 0.9)
extension View {
func applyBackground() -> some View {
ZStack{
backgroundColor
.edgesIgnoringSafeArea(.all)
self
}
}
}
然后将其应用到所有屏幕:
NavigationView {
PrimaryView()
.applyBackground()
DetailView(title: selectedString)
.applyBackground()
}
请注意:某些 SwiftUI 视图有自己的背景颜色,会覆盖您的背景颜色(例如 Form 和 List,具体取决于上下文)
【讨论】: