How do you make a Button conditionally hidden or disabled?

后端 未结 5 421
囚心锁ツ
囚心锁ツ 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:07

    For me it worked perfectly to set the frame's height to zero when you do not want to see it. When you want to have the calculated size, just set it to nil:

    SomeView
        .frame(height: isVisible ? nil : 0)
    

    If you want to disable it in addition to hiding it, you could set .disabled with the toggled boolean.

    SomeView
        .frame(height: isVisible ? nil : 0)
        .disabled(!isVisible)
    

提交回复
热议问题