How do you make a Button conditionally hidden or disabled?

后端 未结 5 451
囚心锁ツ
囚心锁ツ 2021-01-12 14:08

How do I toggle the presence of a button to be hidden or not?
We have the non-conditional .hidden() property; but I need the conditional version.

Note: w

5条回答
  •  死守一世寂寞
    2021-01-12 15:06

    It isn't always going to be a pretty solution, but in some cases, adding it conditionally may also work:

    if shouldShowMyButton {
        Button(action: {
            self.imageDetectionVM.detect(self.selectedImage)
        }) {
            Text("Button")
        }          
    }
    

    There will be an issue of the empty space in the case when it isn't being shown, which may be more or less of an issue depending on the specific layout. That might be addressed by adding an else statement that alternatively adds an equivalently sized blank space.

提交回复
热议问题