I use UIScrollView.appearance().backgroundColor
to change the background color of my ContentView. But unfortunately, that has the side effect that the colors of my ModalView()
are not quite normal anymore.
To solve the problem, I see three possible solutions:
The first option is somehow to assign the UIScrollView.appearance().backgroundColor = UIColor.red
only to the first ScrollView
.
The second would be to find another way to change the background color of my ContentView()
.
And the third option would be to reset the UIScrollView.appearance().backgroundColor
in ModalView()
back to the default setting. (EDIT: I think now that the third option is not possible)
And thanks for every answer
import SwiftUI
struct ContentView: View {
@State private var show_modal: Bool = false
var body: some View {
UIScrollView.appearance().backgroundColor = UIColor.red // This how I change the backgroundcolor of this View
return NavigationView {
ScrollView { // This ScrollView should be affected by the initializer
VStack(spacing: 12) {
HStack {
Text("Only unimportant content")
Spacer()
}
} .padding(.horizontal).padding(.bottom)
}
.navigationBarTitle(Text("Header"))
.navigationBarItems(
leading:
Button(action: { self.show_modal = true }) {
Image(systemName: "plus")
.padding(.all, 10)
} .sheet(isPresented: self.$show_modal) { ModalView() }.padding(.leading, -10)
)
}
}
}
struct ModalView: View { // This should not be affected by the initializer
@Environment(\.presentationMode) var presentationMode
@State private var name: String = ""
var body: some View {
// UIScrollView.appearance().backgroundColor = UIColor.red // If anyone knows the default value, please enter this here
return NavigationView {
Form {
List {
TextField("This is a TextField", text: $name)
}
}
.navigationBarTitle(Text(""), displayMode: .inline)
.navigationBarBackButtonHidden(true)
.navigationBarItems(
leading:
Button(action: { self.presentationMode.wrappedValue.dismiss() }) {
Text("Cancel")
}.padding(.vertical, 5)
)
}
}
}
This is my ModalView()
with the "beautiful" side effects
In this picture, you can see what makes me believe that the third option is not possible:
- The interior of the selected text field is discolored
- The auto-correction suggestions are also discolored
- With dark mode turned on it gets even worse