2016-11-01 10 views
0

私は2 UITextFieldと1 UIButtonコンポーネントを持つxibのViewController(ログインビュー)を持っています。ビュー階層はテキストフィールドの選択時に、キーボードの表示高さを調整します

UIView 
    -> UIScrollView 
     -> UIView (contentView) 
      -> UITextField 
      -> UITextField 
      -> UIButton 

です。私はビューにautolayoutを使用しています。 UIScrollViewは4つの拘束、すなわち先頭、末尾、上部は&、下部はそのスーパービュー(UIViewControllerビュー)に揃えられています。 ContentViewには、先頭、末尾、先頭が&のスーパービュー(UIScrollView)と幅一致UIViewControllerビューの4つの制約があります。

キーボードがUITextFieldの上にならないようにUITextFieldを選択すると、Apple documentationのようにスクロールして上下にスクロールしました。

キーボードが表示されているときスクロールアップがスクロールアップされていますが、キーボードが隠れているときスクロールビューはスクロールバックしません。どんな助けもありがとうございます。

override func viewDidLoad() { 
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil) 
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil) 
} 

func keyboardWillHide(notification: NSNotification) { 

     let contentInsets: UIEdgeInsets = UIEdgeInsets.zero 
     self.scrollView.contentInset = contentInsets 
     self.scrollView.scrollIndicatorInsets = contentInsets 
    } 

func keyboardWillShow(notification: NSNotification) { 

     if let activeField = self.activeField, let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue { 

      let contentInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: keyboardSize.height, right: 0.0) 

      self.scrollView.contentInset = contentInsets 
      self.scrollView.scrollIndicatorInsets = contentInsets 

      var aRect = self.view.frame 

      aRect.size.height -= keyboardSize.size.height 
      if (!aRect.contains(activeField.frame.origin)) { 
       self.scrollView.scrollRectToVisible(activeField.frame, animated: true) 
      } 
     } 
    } 

答えて

0

だから私は問題に直面していた。 contentInset.topを変更した後、scrollViewはキーボードが隠された後元の位置に設定されました。

func keyboardWillShow(notification: NSNotification) { 

     self.scrollView.isScrollEnabled = true 

     var info = notification.userInfo! 
     let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size 
     let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(self.scrollView.contentInset.top, 0.0, keyboardSize!.height, 0.0) 

     self.scrollView.contentInset = contentInsets 
     self.scrollView.scrollIndicatorInsets = contentInsets 

     var aRect : CGRect = self.view.frame 
     aRect.size.height -= keyboardSize!.height 
     if let activeField = self.activeField { 
      if (!aRect.contains(activeField.frame.origin)){ 
       self.scrollView.scrollRectToVisible(activeField.frame, animated: true) 
      } 
     } 
    } 
0

キーボードを使用して、この問題は、Appleのドキュメントで詳細に説明されています

https://developer.apple.com/library/content/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html

代わりに、次のコードを試してください:scrollView contentInset.top0.0なかった

func registerForKeyboardNotifications(){ 
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(notification:)), name:  NSNotification.Name.UIKeyboardWillShow, object: nil) 
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil) 
} 

func deregisterFromKeyboardNotifications(){ 
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil) 
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil) 
} 

func keyboardWasShown(notification: NSNotification){ 
    self.scrollView.isScrollEnabled = true 
    var info = notification.userInfo! 
    let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size 
    let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize!.height, 0.0) 

    self.scrollView.contentInset = contentInsets 
    self.scrollView.scrollIndicatorInsets = contentInsets 

    var aRect : CGRect = self.view.frame 
    aRect.size.height -= keyboardSize!.height 
    if let activeField = self.activeField { 
     if (!aRect.contains(activeField.frame.origin)){ 
      self.scrollView.scrollRectToVisible(activeField.frame, animated: true) 
     } 
    } 
} 

func keyboardWillBeHidden(notification: NSNotification){ 
    var info = notification.userInfo! 
    let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size 
    let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, -keyboardSize!.height, 0.0) 
    self.scrollView.contentInset = contentInsets 
    self.scrollView.scrollIndicatorInsets = contentInsets 
    self.view.endEditing(true) 
    self.scrollView.isScrollEnabled = false 
} 

func textFieldDidBeginEditing(_ textField: UITextField){ 
    activeField = textField 
} 

func textFieldDidEndEditing(_ textField: UITextField){ 
    activeField = nil 
} 
+0

私はkeyboardWillBeHidden機能を提案ごとに変更しましたが、以前の位置に戻るscrollviewは表示されません。 – user2071152

関連する問題