Dynamic UITextView mislocation behavior

此生再无相见时 提交于 2019-12-05 04:07:00

Try bellow code :-

@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
    }
}

Your issue is that the UITextView has conflicting properties:

  • Place on the screen
  • Size

The size being constrained will cause an issue when you need a resizable TextView. Also, when the TextView is resized, its location is being changed in this case.

Alternate method to approach the issue:

Try setting constraints to its location in relation to the bottom of the screen. When the Keyboard appears, you should move the TextView up with it. Also setting constraints on the height of a resizable TextView is bad practice unless you are planning on forcing the user to scroll.

Hope this helps.

If you are using auto layout, you should be updating to constraint instead of updating the textView.frame.Try create a IBOutlet for your textView heightConstraint then set the updated height to it.

IBOutlet weak var textViewHeightConstraint: NSLayoutConstraint!

//calculate the height and update the constant
textViewHeightConstraint.constant = textView.contentSize.height
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!