2015-12-30 51 views
1

在我建立一個示例,我可以發表評論,我使用tableView添加一個單元格,每次我發佈。它的主要處理,但我遇到一個問題,當我想發表評論時,tableview底部是相同的鍵盤高度,但空,如果將其置於零視圖將下移,然後推頂,這是因爲我當我點擊textview寫文本時移動整個視圖。處理tableView contentIn鍵盤時出現

這是空的空間的的tableView下一個預覽(鍵盤的高度,插圖):Preview of image on Google Drive

另一個圖像,以查看是否我提出的底部(內容插圖)= 0時鍵盤被打開:Preview of second image on Google Drive

func keyboardWillShow(sender:NSNotification){ 

     let userInfo: [NSObject : AnyObject] = sender.userInfo! 
     keyboardSize = userInfo[UIKeyboardFrameBeginUserInfoKey]!.CGRectValue.size 
     var contentInsets = UIEdgeInsets() 

     if wasEmoj == true { 
      if (UIInterfaceOrientationIsPortrait(UIApplication.sharedApplication().statusBarOrientation)) { 
       contentInsets = UIEdgeInsetsMake(257.0, 0.0, (self.keyboardSize.height), 0.0); 
      } else { 
       contentInsets = UIEdgeInsetsMake(257.0, 0.0, (self.keyboardSize.width), 0.0); 
      } 
      self.mytableView.contentInset = contentInsets; 
      self.mytableView.scrollIndicatorInsets = contentInsets 
     }else { 
     if (UIInterfaceOrientationIsPortrait(UIApplication.sharedApplication().statusBarOrientation)) { 
      contentInsets = UIEdgeInsetsMake(215.0, 0.0,(self.keyboardSize.height), 0.0); 
     } else { 
      contentInsets = UIEdgeInsetsMake(215.0, 0.0, (self.keyboardSize.width), 0.0); 
     } 

      self.mytableView.contentInset = contentInsets; 
      self.mytableView.scrollIndicatorInsets = contentInsets 

     } 
} 

func textViewDidBeginEditing(textView: UITextView) { 

    self.animateTextField(commentText, up: true, movementDistance: -215, duration: 0.3) 

} 
///- animate the view up or down to adapt with keyboard 
func animateTextField (textField:UITextView,up:Bool,movementDistance:Int,duration:NSTimeInterval){ 

    let movement : Int = (up ? movementDistance : -movementDistance); 
    UIView.beginAnimations("animateTextField", context: nil) 
    UIView.setAnimationBeginsFromCurrentState(true) 
    if up == true { 
     UIView.setAnimationDuration(0.38) 
    }else { 
     UIView.setAnimationDuration(0.3) 
    } 
    self.view.frame = CGRectOffset(self.view.frame, 0, CGFloat(movement)) 
    UIView.commitAnimations() 


} 

回答

1

在這種情況下最好更改tableview的框架。

您可以保留commentView的底部和底部佈局指南之間的約束實例。

@IBOutlet weak var commentViewBottom: NSLayoutConstraint! 

然後在需要時更改約束的常量。

func keyboardWillShow(sender:NSNotification){ 

     if let dic = sender.userInfo { 
      if let keyboardFrame = dic[UIKeyboardFrameEndUserInfoKey]?.CGRectValue { 
       if let duration = dic[UIKeyboardAnimationDurationUserInfoKey]?.doubleValue { 
        commentViewBottom.constant = -keyboardFrame.height 

        UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.5, options: .CurveLinear, animations: {() -> Void in 
         self.view.layoutIfNeeded() 
         }, completion: nil) 
       } 
      } 
     } 
    } 


    func keyboardWillHide(sender: NSNotification) { 
     if let dic = sender.userInfo, let duration = dic[UIKeyboardAnimationDurationUserInfoKey]?.doubleValue { 
       commentViewBottom.constant = 0 

       UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.5, options: .CurveLinear, animations: {() -> Void in 
        self.view.layoutIfNeeded() 
        }, completion: nil) 
     } 
}