swift UIScrollView中的UITextView未滚动到底部

z9ju0rcb  于 2023-06-21  发布在  Swift
关注(0)|答案(1)|浏览(131)

我在scrollview中创建了textview、imageview和label,并添加textView.isScrollEnabled = false来滚动这两个视图。我的问题是,当我在最后一行的textview中点击enter时,我的textview变大了,但没有滚动到底部!光标切断!
就像这样

但我想...

就像这个GIF一样。

我的准则

noteTextView.isScrollEnabled = false

photoImageView.translatesAutoresizingMaskIntoConstraints = false
noteTextView.translatesAutoresizingMaskIntoConstraints = false
bScrollView.translatesAutoresizingMaskIntoConstraints = false
dateLabel.translatesAutoresizingMaskIntoConstraints = false
nView.translatesAutoresizingMaskIntoConstraints = false

view.addSubview(bScrollView)
view.sendSubview(toBack: bScrollView)
bScrollView.addSubview(nView)
nView.addSubview(photoImageView)
nView.addSubview(dateLabel)
nView.addSubview(noteTextView)

bScrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
bScrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
bScrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
bScrollView.topAnchor.constraint(equalTo: topView.bottomAnchor).isActive = true

nView.leadingAnchor.constraint(equalTo: bScrollView.leadingAnchor).isActive = true
nView.trailingAnchor.constraint(equalTo: bScrollView.trailingAnchor).isActive = true
nView.bottomAnchor.constraint(equalTo: bScrollView.bottomAnchor).isActive = true
nView.topAnchor.constraint(equalTo: bScrollView.topAnchor).isActive = true
nView.widthAnchor.constraint(equalTo: bScrollView.widthAnchor).isActive = true

noteTextView.leadingAnchor.constraint(equalTo: nView.leadingAnchor, constant: 18).isActive = true
noteTextView.trailingAnchor.constraint(equalTo: nView.trailingAnchor, constant: -18).isActive = true
noteTextView.bottomAnchor.constraint(equalTo: nView.bottomAnchor).isActive = true
noteTextView.topAnchor.constraint(equalTo: dateLabel.bottomAnchor).isActive = true
noteTextView.heightAnchor.constraint(greaterThanOrEqualToConstant: 0).isActive = true

photoImageView.leadingAnchor.constraint(equalTo: nView.leadingAnchor).isActive = true
photoImageView.trailingAnchor.constraint(equalTo: nView.trailingAnchor).isActive = true
photoImageView.topAnchor.constraint(equalTo: nView.topAnchor).isActive = true
photoImageView.heightAnchor.constraint(equalToConstant: 300).isActive = true

dateLabel.leadingAnchor.constraint(equalTo: nView.leadingAnchor, constant: 12).isActive = true
dateLabel.trailingAnchor.constraint(equalTo: nView.trailingAnchor, constant: -12).isActive = true
dateLabel.topAnchor.constraint(equalTo: photoImageView.bottomAnchor).isActive = true
dateLabel.heightAnchor.constraint(equalToConstant: 30).isActive = true
sdnqo3pr

sdnqo3pr1#

我在创建消息应用程序时遇到了同样的问题。结果发现,在向textView添加换行符时,scrollView没有收到布局更改的通知。我通过确保scrollView在文本更改时布局其子视图来解决这个问题。

func textViewDidChange(_ textView: UITextView) {
    yourScrollView.layoutIfNeeded()
}

相关问题