【问题标题】:UIScrollView with NSLayoutConstraint带有 NSLayoutConstraint 的 UIScrollView
【发布时间】:2018-06-20 18:27:16
【问题描述】:

UIScrollView 不滚动子视图,但确实显示了滚动条。

这就是我想要做的事情

class SetupViewController: UIViewController {

    let scrollView = UIScrollView()
    let pageLabel = UILabel()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupViews()
        self.setupConstraints()
        self.setText()
    }

    func setupViews() {
        self.scrollView.backgroundColor = .red
        self.scrollView.translatesAutoresizingMaskIntoConstraints = false
        // Page Label
        self.pageLabel.font = UIFontLocalized(englishFontSize: 22, arabicFontSize: 22)
        self.pageLabel.textAlignment = .center
        self.pageLabel.translatesAutoresizingMaskIntoConstraints = false
    }

    func setupConstraints() {
        // Add To Sub Views
        self.view.addSubview(self.pageLabel)
        self.view.addSubview(self.scrollView)
        // Page Label
        NSLayoutConstraint(item: self.pageLabel, attribute: .top, relatedBy: .equal, toItem: self.topLayoutGuide, attribute: .bottom, multiplier: 1.0, constant: 30.0).isActive = true
        NSLayoutConstraint(item: self.pageLabel, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1.0, constant: 20.0).isActive = true
        NSLayoutConstraint(item: self.pageLabel, attribute: .width, relatedBy: .equal, toItem: self.view, attribute: .width, multiplier: 1.0, constant: -40.0).isActive = true
        NSLayoutConstraint(item: self.pageLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 36.0).isActive = true
        // Scroll View
        NSLayoutConstraint(item: self.scrollView, attribute: .top, relatedBy: .equal, toItem: self.line1, attribute: .bottom, multiplier: 1.0, constant: 0.0).isActive = true
        NSLayoutConstraint(item: self.scrollView, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1.0, constant: 0.0).isActive = true
        NSLayoutConstraint(item: self.scrollView, attribute: .width, relatedBy: .equal, toItem: self.view, attribute: .width, multiplier: 1.0, constant: 0.0).isActive = true
        NSLayoutConstraint(item: self.scrollView, attribute: .height, relatedBy: .equal, toItem: self.view, attribute: .height, multiplier: 1.0, constant: -100.0).isActive = true
        self.scrollView.contentSize.height = 2000

        // NSLayoutConstraint for rest of elements are removed in this example code.
    }
}

我可以确认 UIScrollView 已添加到视图中,因为我通过为滚动视图提供背景红色进行检查,并且它确实在正确的位置显示了背景红色。我的问题是子视图不移动,只有滚动条在移动。

这可能是什么问题?

注意:所有 UIKit 元素都存在 NSLayoutConstraint,我没有在代码中添加它。

【问题讨论】:

  • 我只是快速浏览了代码,但我认为问题可能是因为您将滚动视图(子视图)的内容限制在滚动视图之外的 topLayoutGuide。而是尝试将子视图约束为超级视图。
  • stackoverflow.com/a/48219419/9086770 我建议您通读一遍,不仅要让这个滚动视图正常工作,还要了解如何正确使用 UIScrollView 的约束。但是,我们目前无法回答您的问题,因为您没有显示滚动视图的子视图的约束,这无疑会导致您的问题。您没有正确地将滚动视图的子视图的约束锚定到滚动视图本身的边界,因此,滚动视图永远不会滚动。
  • 我已经解决了这个问题。感谢@Stanislav Goryachev 的指针。问题是,在某些约束中,我将控制器的视图称为超级视图,而我必须使用 ScrollView 作为超级视图。更改约束中的超级视图解决了我的问题。

标签: swift uiscrollview swift4


【解决方案1】:

我已经解决了这个问题。正如评论中所指出的,该问题与设置为self.view 的约束中的superview 相关,将其更改为self.scrollView 解决了该问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-26
    相关资源
    最近更新 更多