ScrollView Insets无法调整KeyboardHide功能(SWIFT3)

时间:2016-11-07 08:07:37

标签: ios swift xcode swift3

在swift3中调用键盘隐藏功能时,ScrollView的Insets似乎无法在UIEdgeInsets.Zero上自行更新,但相同的代码在swift 2.2中完美执行

 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()
            }
        }
    }

任何想法......?

2 个答案:

答案 0 :(得分:0)

尝试使用swift 3.0,

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

希望它会对你有所帮助。

答案 1 :(得分: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()
        }