2014-09-23 83 views
0

我正在嘗試在鍵盤顯示上調整UITextView的大小。但是,我的下面的代碼行爲很奇怪。文本視圖跳轉到新的高度,然後再回到原始高度。任何幫助在這裏將不勝感激。調整UITextView的高度動畫

//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) 
    } 
+0

我遇到你的代碼,我沒有看到這樣的行爲(而另一個問題這裏所說http://stackoverflow.com/questions/26004080/keyboardwillshow-will-only-work-from-the-second這是否會在沒有動畫的情況下發生,只需執行'self.notes.frame = CGRectMake(self.notes.frame.origin.x,self.notes.frame.origin.y,self.notes.frame。寬度,(notification.userInfo?[UIKeyboardFrameEndUserInfoKey]作爲NSValue).CGRectValue()。origin.y - 10)'? – abinop 2014-09-23 20:44:08

+0

@abinop是的,高度仍然回到原來的高度。 – steventnorris 2014-09-23 21:09:45

回答

0

我通過調整滾動插入而不是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!) 
     } 
    }