本文介绍了Swift:键盘显示时向上滚动视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个scrollView,我想在键盘显示时向上滚动。

I have a scrollView that i want to scroll up when the keyboard is shown.

键盘显示时出现此错误:

I have a crash with this error when the keyboard show :

2014-09-29 14:48:50.738 swrd [1563:472888] - [swrd.EditPhotoViewController keyboardWasShown]:无法识别的选择器发送到实例0x14ed36640

2014-09-29 14:48:50.738 swrd[1563:472888] -[swrd.EditPhotoViewController keyboardWasShown]: unrecognized selector sent to instance 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()

}


推荐答案

在您的代码中, keyboardWasShown keyboardWasHidden 每个接受一个参数, NSNotification 。您需要使用冒号在 addObserver 中终止选择器,以便传递它。即, keyboardWasShown keyboardWasShown:是不同的选择器。

In your code, keyboardWasShown and keyboardWasHidden each take an argument, the NSNotification. You need to terminate your selectors in addObserver with a colon so that it gets passed. I.e., keyboardWasShown and keyboardWasShown: are different selectors.

NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWasShown:", name: UIKeyboardDidShowNotification, object: nil)

这篇关于Swift:键盘显示时向上滚动视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 16:08