Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift : scroll the view up when keyboard shows

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

}
like image 304
jmcastel Avatar asked Sep 29 '14 12:09

jmcastel


People also ask

How do I scroll up when keyboard appears in IOS?

There are two things to do in keyboardWasShown to scroll the text view. First, set the text view's content inset so the bottom edge is the keyboard's height. Second, set the scroll indicator insets to the text view's content inset. if let keyboardRect = info?[UIResponder.

How do I move view up when keyboard appears in IOS SwiftUI?

Moving SwiftUI View Up When Keyboard AppearsCreate keyboard height state. SwiftUI will automatically update the view whenever the keyboard height changes. Add padding to the bottom of the view, which will make it move up and down with the keyboard.

How can I make a Uitextfield move up when the keyboard is present on starting to edit?

For example: You could position your UITextView in a UIView, say called textViewWrapperView - which would be positioned in your viewDidLoad function. Then in keyboardWillShow and keyboardWillHide instead of altering the position of self. frameView. frame you would alter the position of self.


Video Answer


2 Answers

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)
like image 133
jefflovejapan Avatar answered Sep 24 '22 18:09

jefflovejapan


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.

like image 34
Zorayr Avatar answered Sep 23 '22 18:09

Zorayr