Swift: 当键盘显示时,向上滚动视图

14

我有一个scrollView,当键盘弹出时,我想要将其向上滚动。

当键盘出现时,我遇到了以下错误:

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

}
3个回答

11

在你的代码中,keyboardWasShownkeyboardWasHidden都带有一个参数,即 NSNotification。你需要在 addObserver 中使用冒号来终止你的选择器,以便它被传递。例如,keyboardWasShownkeyboardWasShown: 是不同的选择器。

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

10

以下是自包含的解决方案:

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;
}

使用contentInset变量来调整内容插图。


1
感谢您包含了所有的代码,非常棒,谢谢! - Dan Rosenstark

3

在我的情况下,需要对上述代码进行一些更改:

1-首先,您需要在视图中放置一个ScrollView,并设置以下约束条件:

enter image description here

重要的是,您的ScrollView应该比视图更大。

2-放置您的TextField和其他组件。

3-使用@IBOutlet将ScrollView链接到ViewController。

4-向ScrollView添加Delegate:

class ViewController: UIViewController, UITextFieldDelegate,     UIScrollViewDelegate {
...

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-添加代码以滚动并计算键盘大小。

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

当键盘出现时会出现闪烁效果。第一个视图向上移动,然后到达点击的字段。 - Manu Gupta

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接