Swift : scroll the view up when keyboard shows

三世轮回 提交于 2019-12-20 18:19:28

问题


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]: 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()

}

回答1:


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)



回答2:


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.




回答3:


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:

It's important your scrollView be grater than view.

2 - Put your TextField's and other components you need.

3 - Link ScrollView to the ViewController with @IBOutlet

4 - Add the Delegate to ScrollView:

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

5 - Add the Observer's:

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.

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


来源:https://stackoverflow.com/questions/26100545/swift-scroll-the-view-up-when-keyboard-shows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!