我试图有一个类似于iPhone消息的textview,其中textview最初有一个约束(高度< = 100)和scrollEnabled = false 这是项目的链接:
https://github.com/akawther/TextView
https://github.com/akawther/TextView
文本视图的高度根据内容大小增加,如左图所示,直到达到100的高度,然后scrollEnabled设置为true.它完美无缺,直到我点击右下角的“发送”按钮,其中textView应该变为空并返回到原始高度并且scrollEnabled变为false.中间的图像显示了单击按钮时发生的情况.当我开始输入时,文本视图向下移动,就像您在右侧的最后一张图片中看到的那样.
我希望能够单击按钮并消除中间图像上显示的行为,我该如何解决这个问题?
import UIKit class ViewController: UIViewController,UITextViewDelegate { @IBOutlet weak var bottomConstraint: NSLayoutConstraint! @IBOutlet weak var messageTextView: UITextView! @IBOutlet weak var parent: UIView! let messageTextViewMaxHeight: CGFloat = 100 override func viewDidLoad() { super.viewDidLoad() self.messageTextView.delegate = self } @IBAction func Reset(sender: AnyObject) { messageTextView.text = "" messageTextView.frame.size.height = messageTextView.contentSize.height messageTextView.scrollEnabled = false self.parent.layoutIfNeeded() } func textViewDidChange(textView: UITextView) { if textView.frame.size.height >= self.messageTextViewMaxHeight { textView.scrollEnabled = true } else { textView.scrollEnabled = false textView.frame.size.height = textView.contentSize.height } } }
您可以通过在github项目中执行以下步骤来复制我的问题:
1.继续输入单词并按下输入,直到你开始看到滚动
2.单击按钮,您将看到textview以蓝色显示
容器.这是我要消除的问题!
解决方法
试试下面的代码: –
@IBAction func Reset(sender: AnyObject) { messageTextView.scrollEnabled = false messageTextView.text = "" messageTextView.frame.size.height = messageTextView.contentSize.height parent.frame.size.height = 20 self.view.layoutIfNeeded() } func textViewDidChange(textView: UITextView) { if textView.contentSize.height >= self.messageTextViewMaxHeight { textView.scrollEnabled = true } else { textView.frame.size.height = textView.contentSize.height textView.scrollEnabled = false } }