添加 UI 包装器时,我无法使用间隔符将文本视图推送到我的 swift ui VStack 视图的顶部

I am unable to use a spacer to push a text view to the top of my swift ui VStack view, when adding UI wrappers

提问人:Emere 提问时间:12/30/2022 更新时间:12/30/2022 访问量:139

问:

我在下面有一段代码,但问题是文本字段位于视图的中间,当我在下面添加间隔符以将文本字段推送到视图顶部时,它不会将文本字段推送到视图顶部,而是仅在我指定间隔符的 minLength 时移动,但我们知道这不是正确的方法,因为不同的手机具有不同的维度。因此,有人可以对此提供适当的修复程序或找出阻碍 spacer() 工作的原因吗?

import SwiftUI

struct FocusedTextField: UIViewRepresentable {
    class Coordinator: NSObject, UITextFieldDelegate {
        var parent: FocusedTextField

        init(_ parent: FocusedTextField) {
            self.parent = parent
        }

        func textFieldDidEndEditing(_ textField: UITextField) {
            parent.text = textField.text ?? ""
        }
    }

    @Binding var text: String
    @Binding var isFocused: Bool

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    func makeUIView(context: UIViewRepresentableContext<FocusedTextField>) -> UITextField {
        let textField = UITextField(frame: .zero)
        textField.delegate = context.coordinator
        textField.textColor = .black
        textField.placeholder = "Enter some text"
        return textField
    }

    func updateUIView(_ uiView: UITextField, context: UIViewRepresentableContext<FocusedTextField>) {
        uiView.text = text
        if isFocused && !uiView.isFirstResponder {
            uiView.becomeFirstResponder()
        }
    }
}

struct testList: View {
    @State private var text: String = ""
    @State private var isFocused: Bool = false

    var body: some View {
        VStack{
            
            FocusedTextField(text: $text, isFocused: $isFocused)
            
        }
        .onAppear {
            self.text = ""
            self.isFocused = true
        }
        .padding()
    }
}


struct textList_Previews: PreviewProvider {
    static var previews: some View {
        testList()
    }
}

swift swiftui swift3 uikit swiftui-view

评论


答:

0赞 Mina 12/30/2022 #1

问题出在 .如果您使用 from SwiftUI 本身,则可以使用 轻松地将其推送到 中。FocusedTextFieldTextFieldVStackSpacer()

struct ContentView: View {
    @State private var text: String = ""
    
    var body: some View {
        
        VStack{
            TextField("Enter some Text", text: $text)
            Spacer()
        }        
        .onAppear {
            self.text = ""
        }
        .padding()
    }
}

我强烈建议您查看属性包装器以实现焦点状态。 您可以在此处阅读有关它的更多信息: https://developer.apple.com/documentation/swiftui/focusstate@FocusState

0赞 Jatin Bhuva 12/30/2022 #2

对于垂直换行,您需要在 makeUIView 中添加一些代码:

textField.setContentHuggingPriority(.required, for: .vertical)

并添加 testList()spacer()

请尝试以下代码:

struct FocusedTextField: UIViewRepresentable {
    class Coordinator: NSObject, UITextFieldDelegate {
        var parent: FocusedTextField
        
        init(_ parent: FocusedTextField) {
            self.parent = parent
        }
        
        func textFieldDidEndEditing(_ textField: UITextField) {
            parent.text = textField.text ?? ""
        }
    }
    
    @Binding var text: String
    @Binding var isFocused: Bool
    
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
    
    func makeUIView(context: UIViewRepresentableContext<FocusedTextField>) -> UITextField {
        let textField = UITextField(frame: .zero)
        textField.delegate = context.coordinator
        textField.textColor = .black
        textField.placeholder = "Enter some text"
        textField.setContentHuggingPriority(.required, for: .vertical)   //<---------------here
        return textField
    }
    
    func updateUIView(_ uiView: UITextField, context: UIViewRepresentableContext<FocusedTextField>) {
        uiView.text = text
        if isFocused && !uiView.isFirstResponder {
            uiView.becomeFirstResponder()
        }
    }
}

struct testList: View {
    @State private var text: String = ""
    @State private var isFocused: Bool = false
    
    var body: some View {
        VStack{
            
            FocusedTextField(text: $text, isFocused: $isFocused)
            
            Spacer()    //<--------- here
                
            
        }
        .onAppear {
            self.text = ""
            self.isFocused = true
        }
        .padding()
    }
}


struct textList_Previews: PreviewProvider {
    static var previews: some View {
        testList()
    }
}