import SwiftUI
struct ColorSettingView: View {
@Environment(.presentationMode) var presentation
let brPlayer = SoundPlayer()
@State private var bgColor = Color.white
var body: some View {
ZStack{
bgColor
.ignoresSafeArea()
VStack {
ColorPicker("Background Color", selection: $bgColor)
Button (action: {brPlayer.brPlay()
self.presentation
.wrappedValue.dismiss()
}, label: {
Image("Quit")
.resizable()
.aspectRatio(contentMode: .fit)
.padding(/@START_MENU_TOKEN@/[.leading, .bottom, .trailing]/@END_MENU_TOKEN@/)
.frame(width: 100.0, height: 100.0)
})
}
}
}
}
struct ColorSettingView_Previews: PreviewProvider {
static var previews: some View {
ColorSettingView()
}
}
//Another View like ContenViewX
struct ContentViewX: View {
let kiPlayer = SoundPlayer()
@State private var bgColor = Color.white
@State var ColorSettingView = false
var body: some View {
ZStack {
// I also want to change this view as same as ColorSettingView
bgColor
.ignoresSafeArea()
VStack {
Button (action: {kiPlayer.kiPlay()
self.ColorSettingView = true}, label: {Image("Setting")
.resizable()
.aspectRatio(contentMode: .fit)
}
)
.sheet(isPresented: $ColorSettingView, content: {
Game.ColorSettingView()}
)
}
}
}
}
struct ContentViewX_Previews: PreviewProvider {
static var previews: some View {
ContentViewX()
}
}
There is a simpler way to do it, just with Sate and Binding
struct ColorSettingView: View {
@Environment(\.presentationMode) var presentation
@Binding var bgColor : Color
var body: some View {
ZStack{
bgColor
.ignoresSafeArea()
VStack {
ColorPicker("Background Color", selection: $bgColor)
Button (action: {
self.presentation
.wrappedValue.dismiss()
}) {
Text("Quit\n\(bgColor.description)")
.padding()
}
}
}
}
}
struct ContentView: View {
@State var bg : Color = .white
@State var colorSetting = false
var body: some View {
ZStack {
// I also want to change this view as same as ColorSettingView
bg
.ignoresSafeArea()
VStack {
Button("Show\n\(bg.description)") {
colorSetting = true
}
.sheet(isPresented: $colorSetting, content: {
ColorSettingView(bgColor: $bg)}
)
}
}
}
}