【发布时间】:2020-08-28 17:53:11
【问题描述】:
我的问题如下图所示:
我尝试在父视图和文本字段上使用 .fixedSize(horizontal:vertical) 并没有积极的结果。
文本域:
struct TheTextField: UIViewRepresentable {
@Binding var text : String
@Binding var placeholder : String
func makeCoordinator() -> TheTextField.Coordinator {
return TheTextField.Coordinator(parent1: self)
}
func makeUIView(context: UIViewRepresentableContext<TheTextField>) -> UITextView {
let tview = UITextView()
tview.isEditable = true
tview.isUserInteractionEnabled = true
tview.isScrollEnabled = false
tview.text = placeholder
tview.textColor = .gray
tview.font = .systemFont(ofSize: 20)
tview.delegate = context.coordinator
return tview
}
func updateUIView(_ uiView: UITextView, context: UIViewRepresentableContext<TheTextField>) {
}
class Coordinator : NSObject, UITextViewDelegate {
var parent : TheTextField
init(parent1 : TheTextField) {
parent = parent1
}
func textViewDidChange(_ textView: UITextView) {
self.parent.text = textView.text
}
func textViewDidBeginEditing(_ textView: UITextView) {
textView.text = ""
textView.textColor = .label
}
}
}
我希望 textview 不展开并将光标移动到下一行,而不是弄乱父视图。
例子:
Divider()
TheTextField(text: self.$imageToUpload.textCaption, placeholder: self.$placeholder).padding(.horizontal)
Spacer()
【问题讨论】:
-
谢谢!我采用我的代码来实现一个动态高度,该高度将根据文本长度而变化。现在,我每次输入内容时都会遇到背景视图消失和重新出现的问题