2016-11-07 52 views
1

當swift3中調用鍵盤隱藏函數時,似乎ScrollView的Insets不會在UIEdgeInsets.Zero上自我更新,但相同的代碼在swift3中完美執行2.2在KeyboardHide函數(SWIFT3)上不調整ScrollView Insets(SWIFT3)

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

嘗試一下本作SWIFT 3.0,

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

希望它會幫助你。

+0

有什麼意思,在禁用滾動kindly精心製作.. – 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