【SwiftUI】Button - フォロー編 有効/無効 操作-
- リンクを取得
- ×
- メール
- 他のアプリ
Buttonの有効・無効を制御する時について記載します。
下記のようにボタンを画面に表示してみました。コードも下記になります。
・スクリーンショット
・コード
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct ContentView: View { | |
var body: some View { | |
VStack { | |
Text("- Buttons -") | |
.font(.largeTitle) | |
Text("- enable or disable -") | |
Spacer().frame(height: 50.0) | |
Text("Ex1)Tap Enable") | |
ButtonView(text: "Enable Button", action: {}) | |
Spacer().frame(height: 30.0) | |
Text("Ex2)Tap Disable") | |
ButtonView(text: "Disable Button",action: {}) | |
.disabled(true) | |
} | |
} | |
} | |
struct ButtonView: View { | |
let text: String | |
let action: () -> Void | |
@Environment(\.isEnabled) private var isEnabled | |
var body: some View { | |
Button(action: action, label:{ | |
Text(text) | |
.font(.title) | |
.padding(10) | |
.background( | |
isEnabled ? Color.purple: Color.gray) | |
.foregroundColor(isEnabled ? .white : Color.black) | |
}).cornerRadius(10.0) | |
} | |
} |
- リンクを取得
- ×
- メール
- 他のアプリ