键盘从UITextField中解除时,UIScrollView停止滚动

时间:2016-05-27 08:22:43

标签: ios swift uiscrollview

 func keyboardWillShow(notification: NSNotification) {
    //self.scrollView.frame.origin.y = -150
    debugPrint("keyboard show")
    self.scrollView.scrollEnabled = true
    let info : NSDictionary = 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 activeField != nil
    {
        if (!CGRectContainsPoint(aRect, activeField!.frame.origin))
        {
            self.scrollView.scrollRectToVisible(activeField!.frame, animated: true)
        }
    }


}


func keyboardWillHide(notification: NSNotification) {
    // self.scrollView.frame.origin.y = 0
    debugPrint("key board hide")
    let info : NSDictionary = 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.keyboardDismissMode = .Interactive
    self.viewDidLayoutSubviews()
}

我正在计算这样的uiscrollview内容大小,容器是uiscrollview中的视图

 override func viewDidLayoutSubviews() {
    var i = 0
    var contentRect : CGRect = CGRectZero;
    for view in self.container.subviews {
        debugPrint("\(i)")
        contentRect = CGRectUnion(contentRect, view.frame)
        i += 1
    }
    self.scrollView.contentSize = contentRect.size;

}

完成所有这些操作后键盘消失时,scrollview停止滚动。我是ios开发的新手,这个uiscrollview正在杀了我。

0 个答案:

没有答案