[英]UITableView auto scrolling issue on keyboard appearance
我正在使用帶有static cells
(1 個單元格)的普通UITableViewController
。 有幾個UITextfields
在里面,在StackView
因為它是一個 UITableView,滾動是自動處理的,但問題是當點擊 UITextfield 時它會滾動到隨機位置。
第一個截圖是沒有鍵盤的頁面。 第二個屏幕截圖是單擊名字文本字段時(正常行為) 第三個屏幕截圖是單擊姓氏文本字段時(??!! 行為) 第四個屏幕截圖是單擊電子郵件文本字段時(再次 ???!! 行為)
我沒有使用任何外部鍵盤處理庫
我沒有使用任何代碼來添加插圖或處理滾動或處理鍵盤。
文本字段按正確順序放置(TxtFname,TxtLname,TxtFatherName....)
UITextfields 放置在堆棧視圖中。 有人可以告訴我似乎是什么問題嗎?
您可以使用第三方通過滾動 UIView 或 UITableview 來處理鍵盤。
pod 'IQKeyboardManager' 這是可以幫助您管理滾動的第三方。 如果您不想滾動標題,請將其置於與表格視圖不同的視圖中。 並確保您正確使用了自動布局。
func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
if tblProfile.isDecelerating {
view.endEditing(true)
}
}
當您手動滾動 tableview 而不使用鍵盤進入下一個文本字段時,使用此代碼將調用此方法並將隱藏鍵盤。 點擊文本字段后,鍵盤會再次出現。
您可以查看我以前的解決方案。 它完全處理所描述的問題。
我已經為這個問題做了一個創可貼修復,結合了這里發布的不同答案和一些額外的答案。
override func viewWillAppear(_ animated: Bool) {
// super.viewWillAppear(animated)
}
評論viewWillAppear(animated)
會禁用 UITableViewController 的自動滾動。
現在在 ViewDidLoad() 中添加了 2 個通知觀察者
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
以及它們的功能
@objc func keyboardWillShow(notification: NSNotification) {
if !isKeyboardShowing {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
let keyboardHeight = keyboardSize.height
let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as! NSNumber
let curve = notification.userInfo?[UIResponder.keyboardAnimationCurveUserInfoKey] as! NSNumber
adjustTableViewInsets(keyboardHeight: keyboardHeight, duration: duration, curve: curve)
}
}
}
@objc func keyboardWillHide(notification: NSNotification) {
let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as! NSNumber
let curve = notification.userInfo?[UIResponder.keyboardAnimationCurveUserInfoKey] as! NSNumber
adjustTableViewInsets(keyboardHeight: 0, duration: duration, curve: curve)
}
這將確保即使鍵盤可見,也有額外的空間供用戶滾動到頁面底部。
現在我使用IQKeyboardManager
來實現 UP n DOWN 箭頭的額外功能,以移動到下一個UITextField
和 DONE 按鈕以關閉鍵盤。
嗨面臨類似的問題使用
帶有自定義單元格的 UITableview(具有 xib 和多個文本字段)具有下拉列表() 和IQKeyboardManager (第三方使用)
輕松修復
在 viewDidload 里面你剛剛放
yourTextField.inputView.inputView = UIView()
yourTextField.inputView.inputAccessoryView = UIView()
嘗試一下。 我希望這會幫助你: https ://medium.com/@hassanahmedkhan/scrolling-the-hell-out-of-stackview-33d239f9f38e 或者你可以使用這樣的 uitable 視圖: https ://medium.com/@how_noobs_think /uitableviewcontroller-will-automatically-adjust-content-position-relative-to-keyboard-ios-swift-8810a375ffb2
或簡單: 使用 Swift 使用鍵盤移動視圖
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.