如何更改此 UIViewRepresentable 的高度?
How do I change the height of this UIViewRepresentable?
我在网上找到这段代码:
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 视图所做的一样:
CustomTextField(text: $text, placeholder: "")
// constant
.frame(height: 100)
// fill available height
.frame(maxHeight: .infinity)
如果你想让它尊重 intrinsicContentSize
,请查看 this answer
我在网上找到这段代码:
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 视图所做的一样:
CustomTextField(text: $text, placeholder: "")
// constant
.frame(height: 100)
// fill available height
.frame(maxHeight: .infinity)
如果你想让它尊重 intrinsicContentSize
,请查看 this answer