2014-07-02 3 views
8

J'ai une vue de table avec un champ de texte et une vue de texte. J'ai mis ce code comme suggéré par cet exemple de code de pomme https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.htmlTableview fait défiler le contenu lorsque le clavier affiche

@IBOutlet var myTableView: UITableView 
func keyboardWasShown (notification: NSNotification) 
{ 
    println("keyboard was shown") 
    var info = notification.userInfo 
    var keyboardSize = info.objectForKey(UIKeyboardFrameBeginUserInfoKey).CGRectValue().size 

    myTableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardSize.height, 0) 
    myTableView.scrollIndicatorInsets = myTableView.contentInset 
} 

func keyboardWillBeHidden (notification: NSNotification) 
{ 
    println("keyboard will be hidden") 
    myTableView.contentInset = UIEdgeInsetsZero 
    myTableView.scrollIndicatorInsets = UIEdgeInsetsZero 
} 
    override func viewDidLoad() { 

    super.viewDidLoad() 

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

} 

Lorsque je clique sur le « texte » de la vue défilement aller juste au-dessus du haut de l'écran, mais quand je libère le clavier, il reste fait défiler vers le haut. C'est comme si la propriété insets ne pouvait pas être modifiée après la première fois. Quelle est mon erreur?

Répondre

9

Essayez de garder le chemin d'index d'édition editingIndexPathGetting index path et faites défiler tableview à ce chemin d'index

func keyboardWasShown (notification: NSNotification) 
    { 
     println("keyboard was shown") 
     var info = notification.userInfo 
     var keyboardSize = info.objectForKey(UIKeyboardFrameBeginUserInfoKey).CGRectValue().size 

     var contentInsets:UIEdgeInsets 

     if UIInterfaceOrientationIsPortrait(UIApplication.sharedApplication().statusBarOrientation) { 

      contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0); 
     } 
     else { 
      contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.width, 0.0); 

     } 

     myTableView.contentInset = contentInsets 

     myTableView.scrollToRowAtIndexPath(editingIndexPath, atScrollPosition: .Top, animated: true) 
     myTableView.scrollIndicatorInsets = myTableView.contentInset 
    } 
+0

Cela ne fonctionne pas! il reste toujours fixe après le premier défilement. Je ne comprends pas pourquoi les affectations à la fonction keyboardWillBeHidden ne fonctionne pas. Est-ce possible parce que la tableview est construite avec Interface Builder et peut-être y a-t-il une option particulière? – Andorath

+0

@Andorath Pouvez-vous me passer l'échantillon? –

+0

comment puis-je vous poncer l'échantillon? – Andorath

8

utilisation ci-dessous code pour obtenir indexPath et modifier le contenu uitableview Offset basé sur UIKeyboard Taille

func keyboardWillShow(notification: NSNotification) { 
     if ((notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue()) != nil { 
      //self.view.frame.origin.y -= keyboardSize.height 
      var userInfo = notification.userInfo! 
      var keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue() 
      keyboardFrame = self.view.convertRect(keyboardFrame, fromView: nil) 

      var contentInset:UIEdgeInsets = self.tbl.contentInset 
      contentInset.bottom = keyboardFrame.size.height 
      self.tbl.contentInset = contentInset 

      //get indexpath 
      let indexpath = NSIndexPath(forRow: 1, inSection: 0) 
      self.tbl.scrollToRowAtIndexPath(indexpath, atScrollPosition: UITableViewScrollPosition.Top, animated: true) 
     } 
    } 

    func keyboardWillHide(notification: NSNotification) { 
     if ((notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue()) != nil { 
      let contentInset:UIEdgeInsets = UIEdgeInsetsZero 
      self.tbl.contentInset = contentInset 
     } 
    } 
14
override func viewDidLoad() { 
    super.viewDidLoad() 

    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil) 

    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil) 

} 

func keyboardWillShow(_ notification:Notification) { 

    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue { 
     tableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardSize.height, 0) 
    } 
} 
func keyboardWillHide(_ notification:Notification) { 

    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue { 
     tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0) 
    } 
} 
+0

Ceci est la meilleure réponse. Merci. –

Questions connexes