Currently I couldn't find any method to change the color/background of the navigation bar in SwiftUI. Any tips?
相关问题
- SwiftUI: UIImage (QRCode) does not load after call
- In a UIViewControllerRepresentable, how can I pass
- How do you preview a view containing a binding to
- SwiftUI: How to drag and drop a contact from Conta
- How create a MonthPicker in SwiftUI?
相关文章
- SwiftUI ForEach 'identified(by:)' is depre
- SwiftUI automatically scroll to bottom in ScrollVi
- Type of expression is ambiguous without more conte
- SwiftUI - Vertical Centering Content inside Scroll
- NavigationView and NavigationLink on button click
- Angular 6 Material: mat-tab-link be selected by un
- Programmatically detect dark mode in SwiftUI to di
- dyld: Library not loaded SwiftUI when app runs on
In order to change color of navigation bar for all view controllers, you have to set it in
AppDelegate.swift
fileAdd following code to
didFinishLaunchingWithOptions
function inAppDelegate.swift
In here
tintColor
attribute change the background color of the navigation bar.barTintColor
attribute affect to the color of the:Bonus:
Change color of navigation bar title:
titleTextAttributes
affect to the title textI hope it helps. :)
One thing to note that I didn't at first understand: SwiftUI will change the appearance of things like NavigationBar based on whether you are in night mode.
If you want to default it to a different color scheme add
If you create a color scheme using the color set system as outlined in this post: https://www.freecodecamp.org/news/how-to-build-design-system-with-swiftui/ it would apply to the main elements like navigations and tab bars, and allow you to apply different schemes for night/day mode.
Put a
Rectangle
behind yourNavigationView
inside aZStack
:Please see this answer for a solution that does not use
.appearance()
.In short use
UIViewControllerRepresentable
In SwiftUI, at this point we can not change it directly, but you can change navigationBar appearance like this,
I hope this will help you. Thanks!!
Till now there is no definitive API in SwiftUI for this purpose. But you can use the appearance API. Here is a sample code.