2017-10-14 94 views
1

我使用以下代碼滾動顯示/隱藏鍵盤上的UITableView及其頁腳。它在iOS 10中和我一起工作得很好,但是一旦我更新到iOS 11,滾動效果不佳。在顯示/隱藏鍵盤上滾動UITableView iOS11

代碼:

func registerNotificationObservers() 
{  
    NotificationCenter.default.addObserver(self, selector: #selector(ArticleDetailsVC.keyboardWillShow), name: .UIKeyboardWillShow, object: nil) 
    NotificationCenter.default.addObserver(self, selector:#selector(ArticleDetailsVC.keyboardWillHide), name: .UIKeyboardWillHide, object: nil) 
} 

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

@objc func keyboardWillShow(_ notification: Notification) { 
    print("keyboardWillShow") 
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue 
    { 
     if self.commentsTableView.frame.origin.y == 0{ 
      print("keyboardWillShow ..") 
      self.tableViewFooter.frame.origin.y -= keyboardSize.height - 50 
      self.commentsTableView.frame.origin.y -= keyboardSize.height 

     } 


    } 
} 


@objc func keyboardWillHide(_ notification: Notification) { 
    print("keyboardWillHide") 

    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue 
    { 
     if self.commentsTableView.frame.origin.y != 0{ 

      print("keyboardWillHide ..") 
      self.tableViewFooter.frame.origin.y += keyboardSize.height + 50 
      self.commentsTableView.frame.origin.y += keyboardSize.height 
     } 

    } 
} 

希望解決這個問題。

回答

0

(在viewDidLoad()正常)添加以下行後,您的tableView的行爲將是相同的iOS 10的:

if #available(iOS 11.0, *) { 
     self.commentsTableView.contentInsetAdjustmentBehavior = .never 
    } 
+0

試過這個,不工作:( – user1553381

+0

我已經嘗試過你的代碼和我的,tableView將向上移動,當鍵盤顯示在iOS 11上。記住調用你的'registerNotificationObservers()'方法並在'viewDidLoad()'中使用我的代碼。 –

+0

我的代碼運行良好,當應用程序剛剛啓動,但當我隱藏鍵盤'self.view.endEditing(true)'然後嘗試再次顯示它,表不滾動 – user1553381

0

由於UITableView繼承UIScrollView,您可以使用下面這個UIScrollViewDelegate功能:

func scrollViewDidScroll(_ scrollView: UIScrollView) 
{ 
     if scrollView == self.tableView 
     { 
      // show/hide Keyboard 
     } 
} 
0

將斷點放在之後如果讓keyboardSize我有時看到,對於UIKeyboardFrameBeginUserInfoKey鍵盤高度可能是0.0

更改UIKeyboardFrameBeginUserInfoKeyUIKeyboardFrameEndUserInfoKey幫助我在這種情況下

所以,你的代碼:

@objc func keyboardWillShow(_ notification: Notification) { 
    print("keyboardWillShow") 
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue 
    { 
     if self.commentsTableView.frame.origin.y == 0{ 
      print("keyboardWillShow ..") 
      self.tableViewFooter.frame.origin.y -= keyboardSize.height - 50 
      self.commentsTableView.frame.origin.y -= keyboardSize.height 

     } 
    } 
} 

希望這本書能解決你的問題太

對於keyboardWillHide方法,我已經用UI KeyboardFrameBeginUserInfoKey,因爲它沒有問題