[英]Swift : scroll the view up when keyboard shows
I have a scrollView that i want to scroll up when the keyboard is shown. 我有一个scrollView,我想在键盘显示时向上滚动。
I have a crash with this error when the keyboard show : 当键盘显示时,我遇到此错误:
2014-09-29 14:48:50.738 swrd[1563:472888] -[swrd.EditPhotoViewController keyboardWasShown]: unrecognized selector sent to instance 0x14ed36640 2014-09-29 14:48:50.738 swrd [1563:472888] - [swrd.EditPhotoViewController keyboardWasShown]:无法识别的选择器发送到实例0x14ed36640
Here is my code, what's wrong ?: 这是我的代码,出了什么问题?:
func registerForKeyboardNotifications ()-> Void {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWasShown", name: UIKeyboardDidShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillBeHidden", name: UIKeyboardWillHideNotification, object: nil)
}
func deregisterFromKeyboardNotifications () -> Void {
let center: NSNotificationCenter = NSNotificationCenter.defaultCenter()
center.removeObserver(self, name: UIKeyboardDidHideNotification, object: nil)
center.removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
}
func keyboardWasShown (notification: NSNotification) {
let info : NSDictionary = notification.userInfo!
let keyboardSize = info.objectForKey(UIKeyboardFrameBeginUserInfoKey)?.frame
let insets: UIEdgeInsets = UIEdgeInsetsMake(self.scrollView.contentInset.top, 0, keyboardSize!.height, 0)
self.scrollView.contentInset = insets
self.scrollView.scrollIndicatorInsets = insets
self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, self.scrollView.contentOffset.y + keyboardSize!.height)
}
func keyboardWillBeHidden (notification: NSNotification) {
let info : NSDictionary = notification.userInfo!
let keyboardSize = info.objectForKey(UIKeyboardFrameBeginUserInfoKey)?.frame
let insets: UIEdgeInsets = UIEdgeInsetsMake(self.scrollView.contentInset.top, 0, keyboardSize!.height, 0)
self.scrollView.contentInset = insets
self.scrollView.scrollIndicatorInsets = insets
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)
self.registerForKeyboardNotifications()
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(true)
self.deregisterFromKeyboardNotifications()
}
In your code, keyboardWasShown
and keyboardWasHidden
each take an argument, the NSNotification
. 在你的代码中, keyboardWasShown
和keyboardWasHidden
各自接受一个参数NSNotification
。 You need to terminate your selectors in addObserver
with a colon so that it gets passed. 您需要使用冒号在addObserver
终止选择器,以便传递它。 Ie, keyboardWasShown
and keyboardWasShown:
are different selectors. 即, keyboardWasShown
和keyboardWasShown:
是不同的选择器。
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWasShown:", name: UIKeyboardDidShowNotification, object: nil)
See a self-contained solution below: 请参阅下面的独立解决方案:
private func startObservingKeyboardEvents() {
NSNotificationCenter.defaultCenter().addObserver(self,
selector:Selector("keyboardWillShow:"),
name:UIKeyboardWillShowNotification,
object:nil)
NSNotificationCenter.defaultCenter().addObserver(self,
selector:Selector("keyboardWillHide:"),
name:UIKeyboardWillHideNotification,
object:nil)
}
private func stopObservingKeyboardEvents() {
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
}
func keyboardWillShow(notification: NSNotification) {
if let userInfo = notification.userInfo {
if let keyboardSize: CGSize = userInfo[UIKeyboardFrameEndUserInfoKey]?.CGRectValue().size {
let contentInset = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0);
}
}
}
func keyboardWillHide(notification: NSNotification) {
let contentInset = UIEdgeInsetsZero;
}
Use the contentInset
variable to adjust the content insets. 使用contentInset
变量调整内容insets。
In my case, was need some changes in the codes above: 在我的情况下,需要对上面的代码进行一些更改:
1 - First you need put a Scrollview in your view, and set constraints like this: 1 - 首先,您需要在视图中放置Scrollview,并设置如下约束:
It's important your scrollView be grater than view. 重要的是你的scrollView比视图更重要。
2 - Put your TextField's and other components you need. 2 - 放置您需要的TextField和其他组件。
3 - Link ScrollView to the ViewController with @IBOutlet 3 - 使用@IBOutlet将ScrollView链接到ViewController
4 - Add the Delegate to ScrollView: 4 - 将委托添加到ScrollView:
class ViewController: UIViewController, UITextFieldDelegate, UIScrollViewDelegate {
...
5 - Add the Observer's: 5 - 添加观察者:
override func viewWillAppear(animated: Bool) {
self.startKeyboardObserver()
}
override func viewWillDisappear(animated: Bool) {
self.stopKeyboardObserver()
}
private func startKeyboardObserver(){
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil) //WillShow and not Did ;) The View will run animated and smooth
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
}
private func stopKeyboardObserver() {
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
}
6 - Add The code to scroll, calculating KeyboardSize. 6 - 添加要滚动的代码,计算KeyboardSize。
func keyboardWillShow(notification: NSNotification) {
if let userInfo = notification.userInfo {
if let keyboardSize: CGSize = userInfo[UIKeyboardFrameEndUserInfoKey]?.CGRectValue().size {
let contentInset = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0);
self.scrollView.contentInset = contentInset
self.scrollView.scrollIndicatorInsets = contentInset
self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, 0 + keyboardSize.height) //set zero instead self.scrollView.contentOffset.y
}
}
}
func keyboardWillHide(notification: NSNotification) {
if let userInfo = notification.userInfo {
if let keyboardSize: CGSize = userInfo[UIKeyboardFrameEndUserInfoKey]?.CGRectValue().size {
let contentInset = UIEdgeInsetsZero;
self.scrollView.contentInset = contentInset
self.scrollView.scrollIndicatorInsets = contentInset
self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, self.scrollView.contentOffset.y)
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.