2014-07-02 6 views
8

Ich habe eine Tabellenansicht mit einem Textfeld und einer Textansicht. Ich habe diesen Code implementiert wie https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.htmlTabellenansicht Bildlauf Inhalt, wenn Tastatur zeigt

von diesem Apfel Beispielcode vorgeschlagen
@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) 

} 

Wenn ich auf dem „Text“ klicken der Scroll-Ansicht über dem oberen Rand des Bildschirms gehen Sie einfach, aber wenn ich die Tastatur loslassen bleibt hochgescrollt. Es ist genau so, wie die Eigenschaft "inets" nach dem ersten Mal nicht geändert werden kann. Was ist mein Fehler?

Antwort

9

Versuchen Sie, den Bearbeitungsindexpfad zu halten editingIndexPathGetting index path und Scroll-Tableview zu diesem Index Pfad

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

Es funktioniert nicht! Es bleibt nach dem ersten Scrollen immer noch fixiert. Ich verstehe nicht, warum die Zuordnungen bei der Funktion keyboardWillBeHidden nicht funktioniert. Ist es möglich, weil die Tabellenansicht mit Interface Builder erstellt wird und vielleicht gibt es eine bestimmte Option? – Andorath

+0

@Andorath Können Sie mir die Probe übergeben? –

+0

Wie kann ich Ihnen die Probe schleifen? – Andorath

8

Verwenden Sie den folgenden Code indexPath zu erhalten und ändern UITableView Inhalt UIKeyboard Höhenversatz basierend

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

Dies ist die beste Antwort. Vielen Dank. –

Verwandte Themen