【问题标题】:ScrollView Doesn't scroll when keyboard appears If elements in a ContentView出现键盘时 ScrollView 不滚动如果 ContentView 中的元素
【发布时间】:2019-04-16 03:20:50
【问题描述】:

我有UIScrollView,其中有一个UIView(基本上是一个内容视图)。在 contentView 中,我有 UITextFields 和 UIButton。起初,元素直接在UIScrollView 内部。当键盘打开时,如果UITextField 在键盘下方,滚动视图会滚动显示内容。之后,我将它们放在 ContentView 中,它开始不起作用。我究竟做错了什么?

我搜索了下面的帖子,但不确定为什么我的代码不起作用。

How do I scroll the UIScrollView when the keyboard appears?

P.S:我知道社区中有很多示例,但我不确定哪种方法是最好的方法。当这个工作时,这是一个简单而轻松的答案,但不确定为什么不工作。

@objc override func keyboardWillShow(notification: NSNotification){
//Need to calculate keyboard exact size due to Apple suggestions
if let frameValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
    keyboardHeight = frameValue.cgRectValue.size.height

        let contentInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: keyboardHeight, right: 0.0)
        self.scrollView.contentInset = contentInsets
        self.scrollView.scrollIndicatorInsets = contentInsets
}
}

我如何给予约束:

contentView.translatesAutoresizingMaskIntoConstraints = false
scrollView.addSubview(contentView)
contentView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true
contentView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor).isActive = true
contentView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
contentView.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true
scrollView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(scrollView)

contentView.addSubview(TextField1)
contentView.addSubview(TextField2)
contentView.addSubview(button)
scrollView.anchor(header.bottomAnchor, left: self.view.leftAnchor, bottom: self.view.bottomAnchor, right: self.view.rightAnchor, topConstant: 20, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)
TextField1.anchor(contentView.topAnchor, left: self.contentView.leftAnchor, bottom: nil, right: self.contentView.rightAnchor, topConstant: 5, leftConstant: 25, bottomConstant: 0, rightConstant: 25, widthConstant: 0, heightConstant: 70)
TextField1.widthAnchor.constraint(equalTo: self.view.widthAnchor, constant: -50).isActive = true

TextField2.anchor(TextField1.bottomAnchor, left: self.contentView.leftAnchor, bottom: nil, right: self.contentView.rightAnchor, topConstant: 5, leftConstant: 25, bottomConstant: 0, rightConstant: 25, widthConstant: 0, heightConstant: 70)
Button.anchor(TextField2.bottomAnchor, left: self.contentView.leftAnchor, bottom: self.contentView.bottomAnchor, right: self.contentView.rightAnchor, topConstant: 30, leftConstant: 25, bottomConstant: 20, rightConstant: 25, widthConstant: 0, heightConstant: 40)

编辑:我尝试的另一种方法(Apple 推荐)。这个aRect.Contains 总是返回true。

    if let frameValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
    keyboardHeight = frameValue.cgRectValue.size.height

    var contentInsets: UIEdgeInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: keyboardHeight, right: 0.0)
    scrollView.contentInset = contentInsets
    scrollView.scrollIndicatorInsets = contentInsets

    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your application might not need or want this behavior.
    //CGRect(x: 0, y: self.header.frame.maxY, width: self.view.frame.width, height: self.view.frame.height - self.header.frame.maxY)
    var aRect: CGRect = self.view.frame
    aRect.size.height -= keyboardHeight
    aRect.size.height -= header.frame.maxY
    if !aRect.contains(activeTextField.frame.origin) {
        var scrollPoint = CGPoint(x: 0.0, y: activeTextField.frame.origin.y - (keyboardHeight))
        scrollView.setContentOffset(scrollPoint, animated: true)
    }
    }

【问题讨论】:

    标签: ios swift uiscrollview uitextfield


    【解决方案1】:

    实现这一点的简单方法是使用 IQKeyboardManager 库。

    查看链接:https://github.com/hackiftekhar/IQKeyboardManager

    【讨论】:

    【解决方案2】:

    经过一个小时的工作,我找到了解决方案。我分享答案而不是删除帖子因为我的问题是因为UIScrollView 不满足整个屏幕。 UIScrollView 上方有一个标题和一个标签。社区中的所有答案,针对如果 UIScroll 内容整屏。

           @objc override func keyboardWillShow(notification: NSNotification){
        //Need to calculate keyboard exact size due to Apple suggestions
        if let frameValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
            keyboardHeight = frameValue.cgRectValue.size.height
            let scrollViewRect: CGRect = view.convert(scrollView.frame, from: scrollView.superview)
            let hiddenScrollViewRect: CGRect = scrollViewRect.intersection(frameValue.cgRectValue)
    
            // Figure out where the two frames overlap, and set the content offset of the scrollview appropriately
            let contentInsets: UIEdgeInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: hiddenScrollViewRect.size.height, right: 0.0)
            scrollView.contentInset = contentInsets
            scrollView.scrollIndicatorInsets = contentInsets
    
            var visibleRect: CGRect = activeTextField.frame
            visibleRect = scrollView.convert(visibleRect, from: activeTextField.superview)
            visibleRect = visibleRect.insetBy(dx: 0.0, dy: -5.0)
            scrollView.scrollRectToVisible(visibleRect, animated: true)
        }
    

    【讨论】:

      猜你喜欢
      • 2015-02-02
      • 2018-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-02
      • 2012-07-01
      相关资源
      最近更新 更多