2014-07-02 12 views
8

Mam widok tabeli z polem tekstowym i widokiem tekstowym. I zostały wdrożone kod jak sugeruje to jabłko przykładowy kod https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.htmlTableview przewiń zawartość po wyświetleniu klawiatury

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

} 

Po kliknięciu na „tekst” widoku przewijania przejść tuż powyżej górnej części ekranu, ale kiedy zwolnić klawiaturę pozostaje przewijany w górę. To tak, jak nie można zmodyfikować właściwości insets po raz pierwszy. Jaki jest mój błąd?

Odpowiedz

9

Spróbuj utrzymując ścieżkę indeksu edycji editingIndexPathGetting index path i przewijanie tableview do tej ścieżki indeksu

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

To nie działa! nadal pozostaje niezmienny po pierwszym przewinięciu. Nie rozumiem, dlaczego zadania przy funkcji keyboardWillBeHidden nie działają. Czy to możliwe, ponieważ widok tabeli został zbudowany przy użyciu Kreatora interfejsu i może jest jakaś konkretna opcja? – Andorath

+0

@Andorath Czy możesz podać mi próbkę? –

+0

jak mogę przesłać próbkę? – Andorath

8

skorzystać z poniższego kodu, aby uzyskać Indexpath i zmienić zawartość UITableView offsetowy na podstawie UIKeyboard Wysokość

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

To jest najlepsza odpowiedź. Dzięki. –

Powiązane problemy