Skip to content

Instantly share code, notes, and snippets.

@takoikatakotako
Created October 23, 2024 16:24
Show Gist options
  • Select an option

  • Save takoikatakotako/e5e398182be03e6b9e38143dc4aa8dd3 to your computer and use it in GitHub Desktop.

Select an option

Save takoikatakotako/e5e398182be03e6b9e38143dc4aa8dd3 to your computer and use it in GitHub Desktop.
SwiftUIでButtonを有効にしたり無効にしたりする
import SwiftUI
struct ContentView: View {
@State var enable: Bool = true
var body: some View {
VStack {
Toggle(isOn: $enable) {
Text("isEnable: \(enable.description)")
}
Button {
print("Tapped!!")
} label: {
Text("Tap Me!!(\(enable ? "Enable": "Disable"))")
.foregroundColor(Color.white)
.padding()
.background(enable ? Color.orange: Color(UIColor.lightGray))
}
.disabled(!enable)
}.padding()
}
}
#Preview {
ContentView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment