2014-09-29 21 views
11

Ho una vista a scorrimento che voglio far scorrere verso l'alto quando viene mostrata la tastiera.Swift: scorre la vista verso l'alto quando la tastiera mostra

Ho un incidente con questo errore quando lo spettacolo tastiera:

2014-09-29 14: 48: 50,738 SWRD [1563: 472.888] - [swrd.EditPhotoViewController keyboardWasShown]: selettore non riconosciuto inviato a un'istanza 0x14ed36640

Ecco il mio codice, cosa c'è di sbagliato?

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

} 

risposta

10

Nel codice, keyboardWasShown e keyboardWasHidden ogni ripresa un argomento, il NSNotification. È necessario terminare i selettori in addObserver con due punti in modo che venga superato. I.e., keyboardWasShown e keyboardWasShown: sono diversi selettori.

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

Vedere una soluzione autonoma di seguito:

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

utilizzare la variabile contentInset per regolare i inserti di contenuti.

+1

grazie per aver incluso tutto quel codice, grandi cose, grazie! –

2

Nel mio caso, è stato bisogno di alcuni cambiamenti nei codici di cui sopra:

1 - In primo luogo è necessario mettere uno Scrollview nella vista, e impostare vincoli in questo modo:

enter image description here

E 'importante la tua scrollView è più bella della vista.

2 - Metti i tuoi TextField e altri componenti di cui hai bisogno.

3 - Collegamento ScrollView al ViewController con @IBOutlet

4 - Aggiungere il Delegato alla ScrollView:

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

5 - Aggiungere il dell'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 - Aggiungere il codice per scorrere, calcolando 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) 
     } 
    } 
} 
+0

C'è un effetto sfarfallio quando appare la tastiera. La prima vista si sposta verso l'alto e poi arriva al campo TAPpato –

Problemi correlati