【发布时间】:2021-08-09 23:22:21
【问题描述】:
我在网上找到了这段代码:
struct CustomTextField: UIViewRepresentable {
@Binding var text: String
@State var placeholder: String
func makeCoordinator() -> Coordinator {
Coordinator(text: $text)
}
func makeUIView(context: Context) -> UITextField {
let textField = UITextField()
textField.borderStyle = .roundedRect
textField.placeholder = placeholder
textField.autocapitalizationType = .none
textField.autocorrectionType = .no
textField.spellCheckingType = .no
textField.keyboardType = .URL
textField.frame = CGRect(x: 0, y: 0, width: 20.00, height: 10)
textField.delegate = context.coordinator
return textField
}
func updateUIView(_ view: UITextField, context: Context) {
view.text = text
}
}
extension CustomTextField {
class Coordinator: NSObject, UITextFieldDelegate {
@Binding var text: String
init(text: Binding<String>) {
_text = text
}
func textFieldDidChangeSelection(_ textField: UITextField) {
DispatchQueue.main.async {
self.text = textField.text ?? ""
}
}
}
}
代码运行良好。问题是我找不到合适的方法来增加它的高度。如您所见,我尝试使用CGRect 作为框架,但没有效果。如何更改此自定义 UIViewRepresentable 的大小(特别是在我的特定场景中的高度)?
【问题讨论】:
标签: swiftui uiviewrepresentable