【发布时间】:2017-01-07 07:34:27
【问题描述】:
我目前正在使用 Swift 3 为 iPhone 开发一个应用程序,但是我遇到了滚动视图的问题。 在选择文本字段并出现键盘之前,滚动视图正常工作(即:我可以上下滚动),但是在关闭键盘后,滚动视图不允许我再滚动,这是我的问题。 注意:如果我再次选择一个文本字段并使键盘出现,它可以正常工作,并且一旦再次关闭就会停止工作。
我检查了滚动视图的 isScrollEnabled 属性,它似乎已启用。不幸的是,我仍然不太熟悉滚动视图的所有细节,并且似乎无法弄清楚它为什么停止工作。
任何关于我可以在哪里查看的帮助或指示将不胜感激。
编辑:有很多代码,但这里是与滚动视图和键盘相关的缩小部分:
class ScrollViewController: UIViewController, UITextViewDelegate, UITextFieldDelegate {
//Scroll view
@IBOutlet weak var scrollView: UIScrollView!
//UIView inside the scroll view
@IBOutlet weak var contentView: UIView!
//Save button on the top right corner
@IBOutlet weak var saveButton: UIBarButtonItem!
//Text field being editted
var activeTextField:UITextField?
fileprivate var contentInset:CGFloat?
fileprivate var indicatorInset:CGFloat?
override func viewDidLoad() {
contentInset = scrollView.contentInset.bottom
indicatorInset = scrollView.scrollIndicatorInsets.bottom
NotificationCenter.default.addObserver(self,
selector: #selector(ScrollViewController.keyboardWillShow(_:)),
name: NSNotification.Name.UIKeyboardWillShow,
object: nil)
NotificationCenter.default.addObserver(self,
selector: #selector(ScrollViewController(_:)),
name: NSNotification.Name.UIKeyboardWillHide,
object: nil)
}
func adjustInsetForKeyboardShow(_ show: Bool, notification: Notification) {
let userInfo = notification.userInfo ?? [:]
let keyboardFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
let adjustmentHeight = (keyboardFrame.height + 20) * (show ? 1 : -1)
scrollView.contentInset.bottom = (contentInset! + adjustmentHeight)
scrollView.scrollIndicatorInsets.bottom = (indicatorInset! + adjustmentHeight)
}
func keyboardWillShow(_ notification: Notification) {
adjustInsetForKeyboardShow(true, notification: notification)
}
func keyboardWillHide(_ notification: Notification) {
adjustInsetForKeyboardShow(false, notification: notification)
}
//Tap gesture to dismiss the keyboard
@IBAction func hideKeyboard(_ sender: AnyObject) {
self.view.endEditing(false)
}
deinit {
NotificationCenter.default.removeObserver(self);
}
}
【问题讨论】:
-
代码会很有帮助。
-
键盘的出现可能会改变滚动视图的边界,但没有代码就很难分辨。
标签: ios uiscrollview swift3