本节课演示如何切换部分表单的可见性,从而实现根据用户的选择,给用户提供不同的表单项目。
示例代码:
struct ContentView : View { @State var showingVisible = false //用来标识是否隐藏表单 @State var userName = "" @State var password = "" var body: some View { NavigationView { Form { Toggle(isOn: $showingVisible.animation()) { //使用该视图显示或隐藏表单 if(showingVisible){ Text("Hide Form") } else{ Text("Show Form") } } if(showingVisible) { Section(header: Text("Please enter your information:")) { TextField("Username", text: $userName) SecureField("Password", text: $password) } } }.navigationBarTitle(Text("Profiles")) } } }