我的问题是:我的 UITextView 出现滚动问题。当我输入大量数据时,我可以滚动 UITextView.text,但是如果我转到第二个视图控制器,点击后退按钮,然后返回到第一个视图控制器,则无法滚动 UITextView.text。 我的设置如下: 我有两个视图控制器嵌入在导航控制器中,在第一个和第二个视图控制器之间有一个segue。 第一个视图控制器具有标题、名称标签、UITextView、占位符标签(根据 UITextView 是否有文本隐藏或不隐藏)以及保存按钮。 点击“保存”按钮时,会转到第二个视图控制器。
第二个视图控制器有一个显示“Hello World”的标签。
最后,我有一个单例,用于在发生转场时保存 UITextView.text 并在第一个视图控制器重新获得控制权时重新填充 UITextView.text。 以下是我的代码。
第一个视图控制器是: 导入 UIKit 类 NameViewController: UIViewController {
let backgroundColor = UIColor.systemBackground
lazy var nameLabel: UILabel = {
let label = UILabel()
return label
}()
lazy var textView: UITextView = {
let textView = UITextView()
return textView
}()
private let placeholderLabel: UILabel = {
let placeholderLabel = UILabel()
return placeholderLabel
}()
lazy var saveButton: UIButton = {
let button = UIButton()
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
setUpUI()
addSubViews()
buildConstraints()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
textView.isScrollEnabled = true
textView.isUserInteractionEnabled = true
buildPriorText()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
func setUpUI() {
setUpView()
setUpNameLabel()
setUpTextView()
setUpPlaceHolderLabel()
setUpSaveButton()
}
func setUpView() {
view.backgroundColor = backgroundColor
self.navigationItem.title = "Title"
view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(didTapToDismiss)))
}
func setUpNameLabel() {
nameLabel.backgroundColor = UIColor.white
nameLabel.textColor = UIColor.black
nameLabel.text = "Name: "
nameLabel.textAlignment = .center
nameLabel.font = UIFont(name: "Helvetica Neue", size: 20)
nameLabel.numberOfLines = 0
nameLabel.adjustsFontSizeToFitWidth = true
nameLabel.minimumScaleFactor = 0.1
nameLabel.lineBreakMode = .byWordWrapping
nameLabel.layer.cornerRadius = 5
nameLabel.layer.borderWidth = 1
nameLabel.layer.borderColor = UIColor.label.cgColor
nameLabel.sizeToFit()
}
func setUpSaveButton() {
let addSaveButtonTitle = "Save"
saveButton.setTitle(addSaveButtonTitle, for: .normal)
saveButton.backgroundColor = UIColor.systemBlue
saveButton.titleLabel?.font = UIFont(name: "Helvetica Neue", size: 20)
saveButton.titleLabel?.adjustsFontSizeToFitWidth = true
saveButton.titleLabel?.minimumScaleFactor = 0.1
saveButton.setTitleColor(.white, for: .normal)
saveButton.layer.cornerRadius = 10.00
saveButton.layer.masksToBounds = true
saveButton.addTarget(self, action:#selector(saveButtonTapped), for: .touchUpInside)
var configuration = UIButton.Configuration.plain()
configuration.contentInsets = NSDirectionalEdgeInsets(top: 5, leading: 10, bottom: 5, trailing: 10)
saveButton.configuration = configuration
}
func setUpTextView() {
textView.delegate = self
textView.isScrollEnabled = true
textView.isUserInteractionEnabled = true
textView.backgroundColor = UIColor(named: "SnowColor")
textView.font = UIFont(name: "Arial", size: 20)
textView.textColor = UIColor(named: "BlackColor")
textView.autocorrectionType = .no
textView.textContainerInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)
textView.layer.cornerRadius = 5
textView.layer.borderWidth = 1
textView.layer.borderColor = UIColor.label.cgColor
textView.clipsToBounds = true
textView.sizeToFit()
}
func setUpPlaceHolderLabel() {
placeholderLabel.backgroundColor = UIColor.clear
placeholderLabel.text = NSLocalizedString("enter text", comment: "")
placeholderLabel.textColor = UIColor.lightGray
placeholderLabel.font = UIFont(name: "Arial", size: 20)
placeholderLabel.sizeToFit()
placeholderLabel.frame.origin = CGPoint(x: 5, y: (textView.font?.pointSize)! / 2)
}
func addSubViews() {
view.addSubview(nameLabel)
view.addSubview(textView)
textView.addSubview(placeholderLabel)
view.addSubview(saveButton)
}
func buildConstraints() {
nameLabel.translatesAutoresizingMaskIntoConstraints = false
nameLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
nameLabel.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20).isActive = true
nameLabel.heightAnchor.constraint(equalToConstant: 60).isActive = true
textView.translatesAutoresizingMaskIntoConstraints = false
textView.topAnchor.constraint(equalTo: nameLabel.bottomAnchor, constant: 8).isActive = true
textView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 8).isActive = true
textView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -8).isActive = true
textView.bottomAnchor.constraint(equalTo: saveButton.topAnchor, constant: -8).isActive = true
placeholderLabel.translatesAutoresizingMaskIntoConstraints = false
placeholderLabel.leadingAnchor.constraint(equalTo: textView.leadingAnchor, constant: 10).isActive = true
placeholderLabel.trailingAnchor.constraint(equalTo: textView.trailingAnchor).isActive = true
placeholderLabel.topAnchor.constraint(equalTo: textView.topAnchor).isActive = true
placeholderLabel.heightAnchor.constraint(equalToConstant: 60).isActive = true
saveButton.translatesAutoresizingMaskIntoConstraints = false
saveButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
saveButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
saveButton.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.8).isActive = true
saveButton.bottomAnchor.constraint(equalTo: view.keyboardLayoutGuide.topAnchor, constant: -10).isActive = true
}
func buildPriorText() {
guard let holdVariableSavedTextViewData = Variables.savedTextViewData else {return}
textView.text = holdVariableSavedTextViewData
}
@objc private func didTapToDismiss() {
view.endEditing(true)
}
@objc func saveButtonTapped() {
textView.resignFirstResponder()
if textView.text == "\n" {
segueToSecondVC()
} else if textView.text.isEmpty {
segueToSecondVC()
} else {
Variables.savedTextViewData = textView.text
segueToSecondVC()
}
}
func segueToSecondVC() {
performSegue(withIdentifier: "seguefromnamevctosecondvc", sender: nil)
}
}
extension NameViewController: UITextViewDelegate {
func textViewDidChange(_ textView: UITextView) {
if textView.text.isEmpty {
placeholderLabel.isHidden = false
textView.backgroundColor = UIColor.white
} else {
placeholderLabel.isHidden = true
textView.backgroundColor = UIColor.lightGray
}
}
func textViewDidBeginEditing(_ textView: UITextView) {
textView.backgroundColor = UIColor.lightGray
placeholderLabel.isHidden = true
}
func textViewDidEndEditing(_ textView: UITextView) {
if textView.text.isEmpty {
placeholderLabel.isHidden = false
textView.backgroundColor = UIColor.white
} else {
placeholderLabel.isHidden = true
textView.backgroundColor = .lightGray
}
}
}
第二个视图控制器是:SecondViewController: 导入 UIKit 类 SecondViewController: UIViewController { 惰性变量标签:UILabel = { 让标签 = UILabel() 退货标签 }()
override func viewDidLoad() {
super.viewDidLoad()
setUpUI()
addSubViews()
addConstraints()
label.text = "Hello World"
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
func addSubViews() {
view.addSubview(label)
}
func setUpUI() {
setUpLabel()
}
func setUpLabel() {
label.backgroundColor = UIColor.white
label.textColor = UIColor.black
}
func addConstraints() {
label.translatesAutoresizingMaskIntoConstraints = false
label.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
label.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
label.heightAnchor.constraint(equalToConstant: 100).isActive = true
label.widthAnchor.constraint(equalToConstant: 100).isActive = true
}
}
单例位于 Swift 文件中,代码为 进口基金会 结构变量{ 静态变量保存的TextViewData:字符串? }
我在 inputText.text 字段中输入了大量数据,并且能够上下滚动并进行更正。我选择了“保存”按钮,我的文本被保存在单例中,第二个视图控制器负责并按预期显示标签。然后我选择后退按钮并返回到第一个视图控制器,我的原始文本按预期显示在 inputText.text 中。我想进行更正,但无法上下滚动以进行必要的更正。我尝试了高度和宽度约束的多种变化,但滚动问题没有得到解决。我能够通过从子视图中删除占位符标签及其约束来纠正问题,但是当我将占位符标签添加回子视图时,问题又出现了。 我添加了将 UITextView 带到前面的逻辑,认为占位符标签位于 UITextView 的顶部,但这并没有解决问题。 我验证了当 inputText.text 不为空并重新测试时,占位符标签被隐藏,但这并没有纠正问题。
您应该将
placeholderLabel
添加到 textView
的 placeholderLabel
,而不是将 view
添加到 NameViewController
:
将此行 textView.addSubview(placeholderLabel)
更改为 view.addSubview(placeholderLabel)
。 UITextView 中可能存在隐藏的逻辑与其子视图交互,这会导致您的错误