2016-11-07 5 views
1

swift3でKeyboard Hide関数が呼び出されたときに、ScrollViewのInsetsがUIEdgeInsets.Zeroで更新されないようですが、同じコードがswiftで完全に実行される2.2KeyboardHide関数(SWIFT3)でScrollView Insetsが調整されない

if isViewLoaded && view.window != nil { 
     if(scrollView != nil) 
     { 
      scrollView!.contentInset = UIEdgeInsets.zero 
      scrollView!.scrollIndicatorInsets = UIEdgeInsets.zero 
     } 
    } 

または

 if isViewLoaded && view.window != nil { 
     if let userInfo = notification.userInfo { 
      if let keyboardSize: CGSize = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.size { 

       if(scrollView != nil) 
       { 
        let contentInsets = UIEdgeInsetsMake(keyboardSize.height, 0.0, 0.0, 0.0) 
        scrollView!.contentInset = contentInsets 
        scrollView!.scrollIndicatorInsets = contentInsets 
       } 

       self.scrollView?.layoutIfNeeded() 
      } 
     } 
    } 

任意の考え..?

答えて

0

var contentInsets = UIEdgeInsetsZero 
self.scrollView.contentInset = contentInsets 
self.scrollView.scrollIndicatorInsets = contentInsets 
self.scrollView.scrollEnabled = false 

、SWIFT 3.0のためにこれを試してみてください、それはあなたを助けることを願っています。

+0

スクロールを無効にする点は、親切に丁寧に説明します。 – iSwift

+0

実際にはその行は必要ありません。最初の3行だけを追加することもできます。 @Kedar – KAR

+0

は違いがありません – iSwift

0
func keyboardWillShow(_ notification: Notification) { 

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

      UIView.beginAnimations(nil, context: nil) 
      UIView.setAnimationBeginsFromCurrentState(true) 
      var contentInset = self.scrollView!.contentInset 
      contentInset.bottom = keyboardSize.height + adjustHeight 
      self.scrollView!.contentInset = contentInset 
      UIView.commitAnimations() 
     } 

    } 
func keyboardWillHide(_ notification: Notification) { 

      UIView.beginAnimations(nil, context: nil) 
      UIView.setAnimationBeginsFromCurrentState(true) 
      var contentInset = self.scrollView!.contentInset 
      contentInset.bottom = 0 
      self.scrollView!.contentInset = contentInset 

      UIView.commitAnimations() 
     } 
+0

nopeはうまくいきません。また、なぜscrollviewのcontentinsetを保持しているのだろうかと疑問に思っています。次に、scrollviewのcontentinsetにそれを割り当てるために続けます。 UIEdgeInsetsMake(0.0、0.0、keyboardSize.height、0.0)として直接設定することができます。 – iSwift

関連する問題