[SwiftUI] NavigationView 및 Form의 배경색 변경
NavigationView
& Form
를 수정해야 합니다.NavigationView {
VStack(alignment: .leading, spacing: 0.0) {
Form {
Section {
NavigationLink(destination: EmptyView()) {
Text("-")
}
...
}
}.backgroundColor(.black) 👈
}
.navigationBarTitle("Settings", displayMode: .inline)
.navigationBarColor(.black, tintColor: .white) 👈
}
NavigationView 수정
struct NavigationBarModifier: ViewModifier {
var backgroundColor: UIColor?
init(backgroundColor: UIColor?, tintColor: UIColor?) {
self.backgroundColor = backgroundColor
let coloredAppearance = UINavigationBarAppearance()
coloredAppearance.configureWithTransparentBackground()
coloredAppearance.backgroundColor = .clear
coloredAppearance.titleTextAttributes = [.foregroundColor: tintColor as Any]
coloredAppearance.largeTitleTextAttributes = [.foregroundColor: tintColor as Any]
UINavigationBar.appearance().standardAppearance = coloredAppearance
UINavigationBar.appearance().compactAppearance = coloredAppearance
UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance
UINavigationBar.appearance().tintColor = tintColor
}
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: Color?, tintColor: Color?) -> some View {
self.modifier(NavigationBarModifier(backgroundColor: UIColor(backgroundColor ?? .white), tintColor: UIColor(tintColor ?? .black)))
}
}
양식 수정
struct FormModifier: ViewModifier {
init(backgroundColor: UIColor?) {
UITableView.appearance().backgroundColor = backgroundColor
}
func body(content: Content) -> some View {
content
}
}
extension Form {
func backgroundColor(_ backgroundColor: Color?) -> some View {
self.modifier(FormModifier(backgroundColor: UIColor(backgroundColor ?? .white)))
}
}
Reference
이 문제에 관하여([SwiftUI] NavigationView 및 Form의 배경색 변경), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/shohe/swiftui-change-background-color-of-navigationview-form-3lpf텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)