2017-02-21 79 views
1

我想滑動一個文本框,它位於tableview的底部(實際上,在其頁腳視圖中)。所以我嘗試了兩種方法:鍵盤顯示時滑動tableview

  1. 要動態自動佈局約束的tableview

仍然

  • 動畫內容的插圖,它們的非工作或部分工作。目前,我不能對此進行測試在真實的iPad(應用程序是iPad的唯一),我在模擬器測試,但我認爲這樣的事情應該工作:

    1. 動畫contentInsets

      func keyboardWillShow(notification: NSNotification) { 
          if let keyboardHeight = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.height { 
           UIView.animate(withDuration: 0.2, animations: { 
            self.tableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardHeight, 0) 
           }) 
          } 
      } 
      
      func keyboardWillHide(notification: NSNotification) { 
          UIView.animate(withDuration: 0.2, animations: { 
           self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0) 
          }) 
      } 
      
    2. 動畫效果自動佈局約束

    所以在第一個例子中,我從來沒有注意到animati上,也不tableview內容移動。在第二個示例中,我創建了一個自動佈局約束(底部垂直間距)的出口,但我得到了一些奇怪的結果。例如,tableview向上移動,但不完全,只有第一次。

    如果我放了一個斷點,keyboardHeight等於471。我錯過了一些明顯的東西?

  • +0

    使用[IQKeyboardManager](https://github.com/hackiftekhar/IQKeyboardManager#installation- with-cocoapod-) – pkc456

    +0

    @ pkc456對不起,沒有第三方是我的選擇。 – Whirlwind

    +0

    爲什麼你不能在模擬器上測試? – Devster101

    回答

    2

    這就是我試過的,並且如預期的那樣工作得很好。

    你能檢查你的通知中心嗎?

    - 動畫contentInsets

    override func viewWillAppear(_ animated: Bool) { 
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) 
    
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil) 
    } 
    
    func keyboardWillShow(notification: NSNotification) { 
        if let keyboardHeight = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.height { 
         UIView.animate(withDuration: 0.2, animations: { 
          self.tbl.contentInset = UIEdgeInsetsMake(0, 0, keyboardHeight, 0) 
         }) 
        } 
    } 
    
    func keyboardWillHide(notification: NSNotification) { 
        UIView.animate(withDuration: 0.2, animations: { 
         self.tbl.contentInset = UIEdgeInsetsMake(0, 0, 0, 0) 
        }) 
    } 
    

    - 動畫自動佈局約束

    確保你沒有在故事板的UITableView中同時設置了底部佈局和高度的限制。

    但是,還有一個更簡單的方法來做到這一點,只是用UITableViewController。它默認會處理這個功能。這當然是,如果你在使用它沒有問題。

    編輯

    UITextView將扮演一個有點不同UITextFieldUITextView設置爲delegate。並在textViewShouldBeginEditing做所有的表格視圖對齊。

    extension ViewController: UITextViewDelegate { 
        func textViewShouldBeginEditing(_ textView: UITextView) -> Bool { 
         let pointInTable:CGPoint = textView.superview!.convert(textView.frame.origin, to: tbl) 
         tbl.contentOffset = CGPoint(x: tbl.contentOffset.x, y: pointInTable.y) 
         return true 
        } 
    } 
    
    +0

    謝謝,我會盡快檢查計算機,但我會說我把觀察者放在viewDidLoad中,並將它們刪除。不過,我肯定這些方法是有效的,因爲我通過放置斷點來檢查。可悲的是,沒有TableViewController在這裏是一個選項... – Whirlwind

    +0

    奇怪的是,我已經嘗試過這...它和'UITextField'一起使用。但是我正在使用'UITextView',它並不真正起作用。我的意思是,它滑動,而不是適量。至少看起來是這樣。 – Whirlwind

    +0

    你想在表格頁腳視圖中的UITextView向上滾動? – Matt

    0

    我用IQKeyboardManager庫,它可以處理這種行爲:https://github.com/hackiftekhar/IQKeyboardManager

    IQKeyboardManager使您可以防止鍵盤的問題,向上滑動並覆蓋的UITextField/UITextView的,而不需要你輸入任何代碼,無需其他設置。要使用IQKeyboardManager,只需將源文件添加到項目中即可。

    這很容易實現。導入帶豆莢的圖書館,並只是添加,在您的AppDelegate:

    import IQKeyboardManager 
    

    而且在didFinishLaunchingWithOptions:

    IQKeyboardManager.sharedManager().enable = true