美文网首页SwiftUI
.focused()的用法

.focused()的用法

作者: 王勋才 | 来源:发表于2021-09-21 23:11 被阅读0次
    
    
    import SwiftUI
    
    struct ContentView: View {
        enum Field:Hashable{
            case name
            case age
        }
        @State var text:String = ""
        @FocusState var focusedField:Field?
        var body: some View{
            VStack {
                TextField("hi", text: $text, prompt: Text("name"))
                    .focused($focusedField, equals: .name)
                    .textFieldStyle(.roundedBorder)
                .padding()
                
                TextField("hi", text: $text, prompt: Text("age"))
                    .focused($focusedField, equals: .age)
                    .textFieldStyle(.roundedBorder)
                .padding()
                Button {
                    focusedField = .name
                } label: {
                    Text("focus name")
                }
                Button {
                    focusedField = .age
                } label: {
                    Text("focus age")
                }
                Button {
                    focusedField = nil
                } label: {
                    Text("focus nothing")
                }
            }
        }
    }
    
    
    
    import SwiftUI
    
    struct ContentView: View {
       
        @State var text:String = ""
        @FocusState var focusedField:Bool
        var body: some View{
            VStack {
                TextField("hi", text: $text, prompt: Text("name"))
                    .focused($focusedField)
                    .textFieldStyle(.roundedBorder)
                .padding()
                
                
                Button {
                    self.focusedField.toggle()
                } label: {
                    Text("聚焦")
                }
            }
        }
    }
    

    相关文章

      网友评论

        本文标题:.focused()的用法

        本文链接:https://www.haomeiwen.com/subject/omtwgltx.html