选中所有文本时,UITextView会更改位置

时间:2015-06-05 20:35:02

标签: ios swift cocoa-touch uiview ios-simulator

我在键盘后面创建了一个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模拟器中的错误?

1 个答案:

答案 0 :(得分:0)

正如@rdelmar所提议的那样,只需在故事板中关闭自动布局即可解决问题。