动态UITextView错位行为 [英] Dynamic UITextView mislocation behavior

查看:109
本文介绍了动态UITextView错位行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用类似于iPhone消息的textview,其中textview最初具有约束条件(高度< = 100),并且scrollEnabled = false

I am trying to have a textview similar to iPhone messages, where the textview initially has a constraint (height <= 100) and the scrollEnabled = false

这是项目的链接: https://github.com/akawther/TextView

文本内容视图的高度将根据内容大小(如左图所示)增加,直至达到100的高度,然后将scrollEnabled设置为true.直到我单击右下角的发送"按钮,textView都应该为空并返回到原始高度,并且scrollEnabled变为false时,它才能正常工作.中间的图像显示了单击按钮时发生的情况.当我开始输入文字时,如右图所示,文本视图将向下移动. 我希望能够单击该按钮并消除中间图像上显示的行为,如何解决此问题?

The text view increases in height based on the content size as in the image on the left until it reaches the height of 100, then the scrollEnabled is set to true. It works perfectly until I click the "send" button on the lower right where the textView should become empty and go back to the original height and scrollEnabled becomes false. The middle image shows what happens when I click the button. When I start typing the textview moves down as you see in the last image on the right. I want to be able to click the button and eliminate the behavior shown on the middle image, how can I fix this?

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.继续输入单词并按Enter键,直到您开始看到滚动条为止 2.单击按钮,您将看到textview变成蓝色 容器.这是我要消除的问题!

You can replicate my issue by following these steps in the github project: 1. keep typing words and pressing enters until you start seeing the scroll 2. Click the button you will see that the textview goes up in the blue container. This is the issue I want to eliminate!

推荐答案

尝试下面的代码:-

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

这篇关于动态UITextView错位行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆