我创建了跟随键盘的UITextView子类(就像在聊天应用程序中一样)。我将UITextView放置在界面生成器中的任意位置,并在代码中初始化它的位置。除非我在视图中选择了所有文本,否则一切都会正常运行,它会返回到界面生成器中的原始随机位置。我正在iOS模拟器中测试我的应用程序。这是我的代码:

class MessageTextView : UITextView
{
    override func awakeFromNib()
    {
        super.awakeFromNib()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardStateChanged:", name: UIKeyboardDidShowNotification, object: nil)

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardDidHide:", name: UIKeyboardDidHideNotification, object: nil)

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardDidHide:", name: UIKeyboardDidChangeFrameNotification, object: nil)

        backgroundColor = UIColor.greenColor()

    }

    func keyboardFrameChanged(notification : NSNotification)
    {
        println("Keyboard frame changed")
        let rect = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey]?.CGRectValue())!
        frame = CGRect(x: rect.minX, y: rect.minY - 100, width: rect.maxX, height: 100)
    }

    func keyboardDidHide(notification : NSNotification?)
    {
        println("Keyboard hided")
        let size = UIScreen.mainScreen().bounds;
        frame = CGRect(x: 0, y: size.height - 100, width: size.width, height: 100)
    }

    func keyboardStateChanged(notification : NSNotification)
    {
        println("Keyboard showed")
        let rect = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey]?.CGRectValue())!
        frame = CGRect(x: rect.minX, y: rect.minY - 100, width: rect.maxX, height: 100)

    }
}


为什么会有这个问题,我该如何解决?还是仅仅是iOS或iOS模拟器中的错误?

最佳答案

正如@rdelmar所建议的那样,只需在情节提要中关闭自动布局即可解决此问题。

关于ios - 当选择所有文本时,UITextView更改位置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30675409/

10-16 15:07