我有一個向上滾動 scrollView 的擴展,因此它不覆寫 UITextField,但是對于 UITextView,它不會向上滾動。擴展看起來像這樣:
extension SummaryViewController {
///Register for keyboard willHide willShow notifiication
func registerKeyboardNotifications() {
NotificationCenter.default.addObserver(self,
selector: #selector(SummaryViewController.keyboardNotification(notification:)),
name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
}
@objc func keyboardNotification(notification: NSNotification) {
if let userInfo = notification.userInfo {
let endFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
let duration: TimeInterval = (userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
let animationCurveRawNSN = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber
let animationCurveRaw = animationCurveRawNSN?.uintValue ?? UIView.AnimationOptions.curveEaseInOut.rawValue
let animationCurve: UIView.AnimationOptions = UIView.AnimationOptions(rawValue: animationCurveRaw)
if (endFrame?.origin.y)! >= UIScreen.main.bounds.size.height {
//close keyboard
scrollView.contentInset = .zero
} else {
//open keyboard
let height: CGFloat = (notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as? CGRect)!.size.height
scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: height, right: 0)
}
UIView.animate(withDuration: duration,
delay: TimeInterval(0),
options: animationCurve,
animations: { self.view.layoutIfNeeded() },
completion: nil)
}
}
}
我在 viewDidLoad 中呼叫registerKeyboardNotifications()。
uj5u.com熱心網友回復:
對于UITextView它不起作用,所以我用contentOffset這樣做。
斯威夫特 5
extension SummaryViewController {
private func registerKeyboardNotifications() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIControl.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIControl.keyboardWillHideNotification, object: nil)
}
@objc func keyboardWillShow(_ notification: Notification) {
guard let keyboardFrameValue = notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue else { return }
let keyboardFrame = view.convert(keyboardFrameValue.cgRectValue, from: nil)
scrollView.contentOffset = CGPoint(x: 0, y: keyboardFrame.size.height)
}
@objc func keyboardWillHide(_ notification: Notification) {
scrollView.contentOffset = .zero
}
}
uj5u.com熱心網友回復:
一種方法是設定滾動視圖的 contentOffset。當您的用戶點擊 textField 進行編輯時,您可以設定它,如下所示:
func begunEditingTextField(_ sender: UITextField) {
let point = sender(sender, to: self.contentView)
self.scrollView.setContentOffset(CGPoint(x: 0, y: point.y - self.view.frame.height*0.1), animated: true)
}
請注意,我添加了螢屏高度 10% 的空間作為填充。
uj5u.com熱心網友回復:
這是一個非常有用的 pod,可以避免鍵盤覆寫活動欄位。
https://github.com/michaeltyson/TPKeyboardAvoiding
只需通過在 pod 檔案中添加以下行來安裝此 pod:
pod 'TPKeyboardAvoiding'
安裝后UIScrollview,TPKeyboardAvoidingScrollView當您的鍵盤出現時,將您的類更改為現在,它會自動將您的元素移動到鍵盤上方。

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/345142.html
上一篇:為什么ViewController后退按鈕在Xcode上不起作用?
下一篇:如何獲取螢屏大小直到標簽欄底部
