简体   繁体   中英

UITextView changes position when all text selected

I created a subclass of UITextView that follows the keyboard (like in a chat application). I positioned the UITextView in a random place in the interface builder and initialise it's position in code. Everything actually work well except when I select all the text in the view, it goes back to it original random position in the interface builder. I am testing my app in the iOS Simulator. Here is my code:

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)

    }
}

Why am I having this issue and how can I fix it? Or is it just a bug in iOS or the iOS Simulator?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM