with when objective hides appears ios swift keyboard scrollview

ios - when - Swift: desplaza la vista hacia arriba cuando se muestra el teclado



scrollview textfield swift (3)

Tengo un scrollView que quiero desplazar hacia arriba cuando se muestra el teclado.

Tengo un bloqueo con este error cuando el teclado muestra:

2014-09-29 14: 48: 50.738 swrd [1563: 472888] - [swrd.EditPhotoViewController keyboardWasShown]: selector no reconocido enviado a la instancia 0x14ed36640

Aquí está mi código, ¿qué sucede?

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


En su código, keyboardWasShown y keyboardWasHidden toman cada uno un argumento, NSNotification . addObserver terminar sus selectores en addObserver con dos puntos para que se pase. Es decir, keyboardWasShown y keyboardWasShown: son diferentes selectores.

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


Vea una solución autónoma a continuación:

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 la variable contentInset para ajustar las inserciones de contenido.


En mi caso, fue necesario algunos cambios en los códigos anteriores:

1 - Primero necesitas poner una vista de desplazamiento en tu vista y establecer restricciones como esta:

Es importante que su scrollView sea más detallado que la vista.

2 - Ponga su TextField y otros componentes que necesita.

3 - Enlace ScrollView al ViewController con @IBOutlet

4 - Agregue el delegado a ScrollView:

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

5 - Agregue el Observer:

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 - Agregue el código para desplazarse, calculando 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) } } }