How to re-size UITextView when keyboard shown with iOS 7

后端 未结 8 1410
耶瑟儿~
耶瑟儿~ 2020-12-05 03:34

I have a view controller which contains a full-screen UITextView. When the keyboard is shown I would like to resize the text view so that it is not hidden under

8条回答
  •  攒了一身酷
    2020-12-05 04:12

    This is my solution, July 2015 using Swift 1.2 on Xcode 6.4 targeting iOS 7.1 - a combination of several approaches. Borrowed Johnston's keyboard handing Swift code. Its a bit of a hack, but its simple and it works.

    I have a vanilla UITextView inside a single View.

    I did not want to embed it inside a UIScrollView as per Apple's documentation. I just wanted the UITextView re-sized when software keyboard appeared, and resized to original when keyboard was dismissed.

    These are the basic steps:

    1. Set up keyboard notifications
    2. Set up layout constraint in "Interface Builder" (TextView to bottom edge in my case)
    3. Create an IBOutlet for this constraint in the relevant code file so you can adjust it programmatically
    4. Use keyboard notifications to intercept events and get keyboard size
    5. Programmatically adjust constraint IBOutlet using keyboard size to re-size TextView.
    6. Put everything back when keyboard is dismissed.

    So, onto the code.

    I've set up constraint outlet at the top of the code file via the usual drag-drop in interface builder: @IBOutlet weak var myUITextViewBottomConstraint: NSLayoutConstraint!

    I also set up a global variable where I can back up the state of affairs before the keyboard come up: var myUITextViewBottomConstraintBackup: CGFloat = 0

    Implement keyboard notifications, call this function in viewDidLoad or any other startup/setup section:

    func setupKeyboardNotifications() {
    
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWasShown:"), name: UIKeyboardDidShowNotification, object: nil)
    
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillBeHidden:"), name: UIKeyboardWillHideNotification, object: nil)
    
        }
    

    Then these two functions will be called automatically when keyboard is shown/dismissed:

    func keyboardWasShown(aNotification:NSNotification) {
    
        let info = aNotification.userInfo
        let infoNSValue = info![UIKeyboardFrameBeginUserInfoKey] as! NSValue
        let kbSize = infoNSValue.CGRectValue().size
    
        let newHeight = kbSize.height
    
        //backup old constraint size
        myUITextViewBottomConstraintOld = myUITextViewBottomConstraint.constant 
    
        // I subtract 50 because otherwise it leaves a gap between keyboard and text view. I'm sure this could be improved on.
        myUITextViewBottomConstraint.constant = newHeight - 50 
    
    func keyboardWillBeHidden(aNotification:NSNotification) {
        //restore to whatever AutoLayout set it before you messed with it
        myUITextViewBottomConstraint.constant = myUITextViewBottomConstraintOld 
    
    }
    

    The code works, with a minor issue:

    • It's not responsive to the predictive text ribbon above the keyboard opening/closing. I.e. it will take the state of it into account when the keyboard is called up, but if you were to slide it up or down while keyboard is shown the constraint will not be adjusted. It is a separate event that needs to be handled. Its not enough of a functionality hit for me to bother with.

提交回复
热议问题