首页 > 解决方案 > Is there a possibility to deactivate a button in SwiftUI?

问题描述

Is there a possibility to deactivate a button in SwiftUI? Can not find anything?

I want to do a download with Alamofire and then activate the button after successful download.

标签: swiftui

解决方案


您可以使用.disabled修饰符。根据文档:

添加控制用户是否可以与此视图交互的条件。

import SwiftUI

struct ContentView: View {
    @State private var buttonDisabled = true

    var body: some View {
        Button(action: {
            //your action here
        }) {
            Text("CLICK ME!")
        }
        .disabled(buttonDisabled)
    }
}

#if DEBUG
struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentView()
  }
}
#endif

buttonDisabled下载完成后,您可以将State var 设置为 false。


推荐阅读