SwiftUi 네비게이션바 배경색 변경 방법입니다!
struct NavigationBarModifier : ViewModifier {
var backgroundColor : UIColor ?
var titleColor : UIColor ?
init ( backgroundColor: UIColor ? , titleColor: UIColor ? ) {
self . backgroundColor = backgroundColor
let coloredAppearance = UINavigationBarAppearance ( )
coloredAppearance. configureWithTransparentBackground ( )
coloredAppearance. backgroundColor = backgroundColor
coloredAppearance. titleTextAttributes = [ . foregroundColor: titleColor ?? . white]
coloredAppearance. largeTitleTextAttributes = [ . foregroundColor: titleColor ?? . white]
UINavigationBar . appearance ( ) . standardAppearance = coloredAppearance
UINavigationBar . appearance ( ) . compactAppearance = coloredAppearance
UINavigationBar . appearance ( ) . scrollEdgeAppearance = coloredAppearance
}
func body( content: Content ) -> some View {
ZStack {
content
VStack {
GeometryReader { geometry in
Color ( self . backgroundColor ?? . clear)
. frame ( height: geometry. safeAreaInsets. top)
. edgesIgnoringSafeArea ( . top)
Spacer ( )
}
}
}
}
}
extension View {
func navigationBarColor( backgroundColor: UIColor ? , titleColor: UIColor ? ) -> some View {
self . modifier ( NavigationBarModifier ( backgroundColor: backgroundColor, titleColor: titleColor) )
}
}
NavigationView {
}
. navigationBarColor ( backgroundColor: . black, titleColor: . white)
참조자료: https://stackoverflow.com/questions/56505528/swiftui-update-navigation-bar-title-color