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

qxsslcnc  于 2023-01-01  发布在  Swift
关注(0)|答案(2)|浏览(131)

下面我有一段代码,但问题是文本字段位于视图的中间,当我在下面添加空格将文本字段推到视图的顶部时,它不会被推到视图的顶部,而是只在我指定了分隔符的minLength时移动,但我们知道这不是正确的方法,因为不同的音素有不同的维度。因此,有人可以提供一个正确的固定到这个或点什么是阻碍间隔()工作

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()
    }
}
mnemlml8

mnemlml81#

对于垂直换行,您需要在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()
    }
}
643ylb08

643ylb082#

问题出在FocusedTextField上,如果你使用的是来自SwiftUI本身的TextField,你可以很容易地通过使用Spacer()VStack中把它往上推。

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

我强烈建议你研究一下@FocusState属性 Package 器来实现焦点状态。你可以在这里阅读更多关于它的信息:https://developer.apple.com/documentation/swiftui/focusstate

相关问题