[英]UITableViewController not scrolling view when keyboard appears
我有一個UITableViewController
大約有 20 個靜態單元格,其中一些單元格中有UITextField
,有些只是用於使用復選標記進行選擇。 該表大約有 1.5 個視圖,因此需要滾動才能到達較低的文本字段。
當我在表格底部的文本字段中單擊時,鍵盤會按原樣彈出,但隨后會出現在單元格/文本字段上。
我的印象是(來自 Apple 文檔和其他地方) UITableViewController
類會在鍵盤出現在任何方向時自動處理視圖的滾動並向上移動 tableview 以便單元格可見,但這並沒有發生。
IOS 5.1,iPad 縱向。
確保如果您正在覆蓋您調用的viewWillAppear
[super viewWillAppear:animated];
如果不這樣做,滾動視圖將無法正確向上滾動。
迅速
super.viewWillAppear(animated)
我發現這些答案都不是正確的。 過了一會兒,我注意到如果你按下一個控制器它不會工作......但如果你以模態方式呈現它......表格將自動滾動到使用的文本字段。
希望能為任何人節省時間和壓力。
如果 UITableViewController 的自動滾動不適用於單元格中的 UITextFields 或滾動異常,請執行以下步驟。 Swift 5 iOS 13.2 測試 100%
首先實現viewWillAppear但不要調用super.viewWillAppear (這將停止自動滾動)
override func viewWillAppear(_ animated: Bool) {
}
然后讓我們手動滾動。
override func viewWillAppear(_ animated: Bool) {
//Notification center observers
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)),
name: UIResponder.keyboardDidShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)),
name: UIResponder.keyboardWillHideNotification, object: nil)
}
//keybord show action
@objc func keyboardWillShow(notification: Notification) {
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: notification.getKeyBoardHeight, right: 0)
}
//keyboard hide action
@objc func keyboardWillHide(notification: Notification) {
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
extension Notification {
var getKeyBoardHeight: CGFloat {
let userInfo: NSDictionary = self.userInfo! as NSDictionary
let keyboardFrame: NSValue = userInfo.value(forKey: UIResponder.keyboardFrameEndUserInfoKey) as! NSValue
let keyboardRectangle = keyboardFrame.cgRectValue
return keyboardRectangle.height
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self)
}
我自己遇到了這個問題。 除了添加[super viewWillAppear:animated];
調用,您將需要刪除這些行:
[self.tableView setDataSource:self]; [self.tableView setDelegate:self];
因為不再需要它們並且 setDelegate 會干擾鍵盤滾動行為。
我有一個帶有大約20個靜態單元格的UITableViewController
,其中一些單元格內包含UITextField
,有些僅用於選中標記。 該表大約有1.5個視圖,因此需要滾動才能到達較低的文本字段。
當我在表格底部的文本字段中單擊時,鍵盤會按原樣彈出,但隨后會出現在單元格/文本字段上方。
我的印象是(來自Apple文檔和其他地方) UITableViewController
類在鍵盤以任何方向出現並將表視圖上移以使單元可見時自動處理視圖的滾動,但是這種情況沒有發生。
iOS 5.1,iPad Portrait。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.