【发布时间】:2016-05-04 22:04:42
【问题描述】:
我有一个带有 ContentView 的 ScrollView,它有 5 个 UITextField。我已经实现了adjustForKeyboard:
func adjustForKeyboard(notification: NSNotification) {
let userInfo = notification.userInfo!
let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
let keyboardViewEndFrame = view.convertRect(keyboardScreenEndFrame, fromView: view.window)
if notification.name == UIKeyboardWillHideNotification {
scrollView.contentInset = UIEdgeInsetsZero
} else {
scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardViewEndFrame.height, right: 0)
}
scrollView.scrollIndicatorInsets = scrollView.contentInset
}
当然我已经设置了观察者:
override func viewWillAppear(animated: Bool) {
// Add observers for keyboard
let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.addObserver(self, selector: "adjustForKeyboard:", name: UIKeyboardWillHideNotification, object: nil)
notificationCenter.addObserver(self, selector: "adjustForKeyboard:", name: UIKeyboardWillChangeFrameNotification, object: nil)
}
当我从上到下选择 TextFields 时它工作正常,但是当我选择上层 TextField 时,它只移动几个像素。我应该如何改变我的方法?
【问题讨论】:
标签: ios iphone swift uiscrollview uitextfield