ios – Swift:键盘显示时向上滚动视图

前端之家收集整理的这篇文章主要介绍了ios – Swift:键盘显示时向上滚动视图前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个scrollView,当我显示键盘时,我想向上滚动.

键盘显示时我有这个错误的崩溃:

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

这是我的代码,怎么了?

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,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,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)

    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中终止您的选择器,以便它被传递.即,键盘WasShown和keyboardWasShown:是不同的选择器.
NSNotificationCenter.defaultCenter().addObserver(self,selector: "keyboardWasShown:",object: nil)
原文链接:https://www.f2er.com/iOS/330051.html

猜你在找的iOS相关文章