我在SwiftUI中使用UIViewRepresentable包装了UITextfield,但是我无法设置宽度

时间:2020-01-14 07:15:18

标签: swiftui textfield

已更新:我找到了答案并解决了此问题:(FYI:SwiftUI - State change in UITextField causes the frameWidth to grow uncontrolled and ignore bounds/frame

func makeUIView(context: UIViewRepresentableContext<MyField>) -> UITextField {
    let field = UITextField(frame: .zero)
    field.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)

当我在其中输入越来越多的文本时,即使设置了框架宽度,文本字段也会越来越宽。

public struct FoolTextField: UIViewRepresentable {
    @Binding var text: String
    private let textField = UITextField(frame: .zero)

    public init(_ placeholder: String, text: Binding<String>) {
        _text = text
        textField.text = text.wrappedValue
        textField.placeholder = placeholder
        textField.borderStyle = .none
        textField.clearButtonMode = .whileEditing
        textField.returnKeyType = .done
    }
    ...
}


FoolTextField("placeholder", text: $text).frame(width: 200)

0 个答案:

没有答案