0

I am attempting to resize a UITextView on keyboard show. However, my below code is behaving oddly. The text view jumps to the new height, then animates back to it's original height. Any help here would be appreciated.

 //Handles keyboard appearance
    func keyboardWillShow(notification:NSNotification){
        var rightButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: Selector("dismissKeyboard"))
        self.navigationItem.rightBarButtonItem = rightButton
        self.origNoteFrame = notes.frame
        var duration = NSTimeInterval((notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as NSNumber).intValue)
        var options = UIViewAnimationOptions(
            UInt((notification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as NSNumber).integerValue << 16)
        )
        UIView.animateWithDuration(
            duration,
            delay: 0.0,
            options: options,
            animations: {
                self.notes.frame = CGRectMake(self.notes.frame.origin.x, self.notes.frame.origin.y, self.notes.frame.width, (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as NSValue).CGRectValue().origin.y - 10)
            },
            completion: nil)
    }
steventnorris
  • 5,656
  • 23
  • 93
  • 174
  • I have run your code and I do not see such a behavior (rather another problem stated here http://stackoverflow.com/questions/26004080/keyboardwillshow-will-only-work-from-the-second-time-on. Does this happen without the animation, by simply doing `self.notes.frame = CGRectMake(self.notes.frame.origin.x, self.notes.frame.origin.y, self.notes.frame.width, (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as NSValue).CGRectValue().origin.y - 10)` ? – abinop Sep 23 '14 at 20:44
  • @abinop Yes it does. The height still returns to the original height. – steventnorris Sep 23 '14 at 21:09

1 Answers1

0

I solved this by adjusting the scroll insets as opposed to the height of the UITextView.

//Handles keyboard appearance
    func keyboardWillShow(notification:NSNotification){

        //Set up done button
        self.origRightBtn = self.navigationItem.rightBarButtonItem
        var rightButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: Selector("dismissKeyboard"))
        self.navigationItem.rightBarButtonItem = rightButton

        //Move scrolling insets
        var info:Dictionary = notification.userInfo!
        var rect:CGRect = self.view.convertRect((info[UIKeyboardFrameBeginUserInfoKey] as NSValue).CGRectValue(), fromView: nil)
        var insets:UIEdgeInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: rect.size.height - 150, right: 0.0)
        self.notes.contentInset = insets
        self.notes.scrollIndicatorInsets = insets
        var newRect:CGRect = self.view.frame
        newRect.size.height -= rect.height - 150

        //Scroll to selected range
        if(selectedRange != nil)
        {
            self.notes.scrollRangeToVisible(selectedRange!)
        }
    }
steventnorris
  • 5,656
  • 23
  • 93
  • 174