2017-03-29 2 views
0

나는 위쪽, 아래쪽, 꼬리말 및 꼬리표가 달린 전체 화면 UITableView를 가지고 있습니다. 이 ViewController는 내비게이션 컨트롤러 안에 있습니다. 테이블의 맨 아래쪽이 위로 이동하고 나타나는대로 키보드로 움직이기를 원합니다. 다음 코드를 가지고 있습니다 :키보드가 나타나면 UITableView의 크기를 조절하십시오. 애니메이션은 육포입니다.

// MARK: Keyboard 

    func registerObservers() { 
     NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) 
     NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil) 
    } 

    func unregisterObservers() { 
     NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil) 
     NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil) 
    } 

    func keyboardWillShow(_ notification: Notification) { 
     let keyboardFrame = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue 
     let keyboardAnimationDuration = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber 
     let keyboardAnimationCurve = notification.userInfo?[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber 
     tableViewBottomConstraint.constant = keyboardFrame.height 
     UIView.animate(withDuration: TimeInterval(keyboardAnimationDuration), delay: 0, options: [UIViewAnimationOptions(rawValue: UInt(keyboardAnimationCurve))], animations: { 
      self.view.layoutIfNeeded() 
     }, completion: { (finished: Bool) in 
     }) 
    } 

    func keyboardWillHide(_ notification: Notification) { 
     let keyboardAnimationDuration = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber 
     let keyboardAnimationCurve = notification.userInfo?[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber 
     tableViewBottomConstraint.constant = 0 
     UIView.animate(withDuration: TimeInterval(keyboardAnimationDuration), delay: 0, options: [UIViewAnimationOptions(rawValue: UInt(keyboardAnimationCurve))], animations: { 
      self.view.layoutIfNeeded() 
     }, completion: { (finished: Bool) in 
     }) 
    } 

ViewDidLoad에 옵저버를 등록합니다. 키보드가 나타나면 애니메이션이 상당히 흔들리는 것처럼 보입니다. 그러나 해산 애니메이션은 어떤 문제도없는 것처럼 보입니다. 여기서 내가 뭘 잘못하고 있니? 애니메이션 기간 또는 커브를 잘못 설정 했습니까?

+0

자동 –

+0

이 볼 일을 http://stackoverflow.com/questions/594181/making-a-uitableview-scroll-when-text-field-is-selected합니다 –

+0

tableView 제약 조건을 이동하는 대신 tableView의 contentOffset을 이동하는 것을 고려하십시오. 훨씬 더 좋을 것입니다. – Tj3n

답변

1

나는 다음을 수행하여이 작업을 수행 한 :

바닥 제약에 함께 IBOutlet 변수를 선언합니다. 귀하의 경우에는 tableView의 하단 제약 조건 인 적절한 제약 조건을 연결해야합니다.

@IBOutlet weak var bottomConstraint: NSLayoutConstraint! 

조정을 처리하는 함수를 추가하십시오. 마지막으로

func keyboardChangeFrame(_ notification: Notification) { 
    let endFrame = ((notification as NSNotification).userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue 
    bottomConstraint.constant = view.bounds.height - endFrame.origin.y 
    self.view.layoutIfNeeded() 
} 

그리고

,있는 viewDidLoad에 UIKeyboardWillChangeFrame 옵저버를 추가합니다. 대신의 UIViewController의있는 UITableViewController를 사용하는 경우

NotificationCenter.default.addObserver(self 
     , selector: #selector(keyboardChangeFrame) 
     , name: NSNotification.Name.UIKeyboardWillChangeFrame 
     , object: nil) 
관련 문제