我的带有自动布局约束的 Swift 4 UIScrollView 没有滚动

时间:2022-10-16
本文介绍了我的带有自动布局约束的 Swift 4 UIScrollView 没有滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在将视图添加到我的应用程序之前,我创建了一个小型演示游乐场以使其正常工作.

我有一个滚动视图,其中包含许多用于水平滚动的按钮.我知道这些按钮需要进入滚动视图中的容器视图,并且也创建了它.我最初使用自动布局约束来创建所有这些,但现在尝试使用常量来确保内容视图大于滚动视图.但是,按钮仍然不会滚动......我错过了什么吗?滚动视图是否不适用于自动布局?

我也在我的 iPad 上以编程方式完成这一切,所以很遗憾,界面生成器的解决方案不是一个选项......

这里是完整的代码:

导入 UIKit导入游乐场支持类FilterViewController:UIViewController {var filterView: UIView!var 滚动视图:UIScrollView!var 容器视图:UIView!覆盖 func loadView() {过滤视图 = UIView()视图 = 过滤视图view.backgroundColor = #colorLiteral(红色:0.909803926944733,绿色:0.47843137383461,蓝色:0.643137276172638,alpha:1.0)滚动视图 = UIScrollView()scrollView.backgroundColor = #colorLiteral(红色:0.474509805440903,绿色:0.839215695858002,蓝色:0.976470589637756,alpha:1.0)view.addSubview(scrollView)scrollView.translatesAutoresizingMaskIntoConstraints = falsescrollView.topAnchor.constraint(equalTo:view.topAnchor, constant:40).isActive = truescrollView.leadingAnchor.constraint(equalTo:view.leadingAnchor).isActive = truescrollView.widthAnchor.constraint(equalTo:view.widthAnchor).isActive = truescrollView.heightAnchor.constraint(equalToConstant: 200).isActive = truescrollView.isScrollEnabled = true容器视图 = UIView()containerView.backgroundColor = #colorLiteral(红色:0.176470592617989,绿色:0.498039215803146,蓝色:0.756862759590149,alpha:1.0)scrollView.addSubview(containerView)containerView.frame = CGRect(x: 0, y: 0, width: 1080, height: 200)}类按钮{让按钮 = UIButton()初始化(标题文本:字符串){button.backgroundColor = #colorLiteral(红色:0.976470589637756,绿色:0.850980401039124,蓝色:0.549019634723663,alpha:1.0)button.setTitle(titleText, for: .normal)button.frame = CGRect(x: 0, y: 0, 宽度: 200, 高度: 200)}}覆盖 func viewDidLoad() {super.viewDidLoad()让 b1 = Buttons(titleText: "one")让 b2 = Buttons(titleText: "two")让 b3 = Buttons(titleText: "三")让 b4 = Buttons(titleText: "four")让 b5 = Buttons(titleText: "五")让 buttonArray = [b1,b2,b3,b4,b5]var startPoint : CGFloat = 0.0对于按钮数组中的 btn {让 theBtn = btn.buttoncontainerView.addSubview(theBtn)theBtn.frame = CGRect(x: startPoint, y: 0, width: 200, height: 200)起点 += 220}}}让 filterViewController = FilterViewController()PlaygroundPage.current.liveView = filterViewController

谢谢你!这是包含所有自动布局约束的完整(正在运行)迷你项目:

导入 UIKit导入游乐场支持类FilterViewController:UIViewController {var filterView: UIView!var 滚动视图:UIScrollView!var 容器视图:UIView!覆盖 func loadView() {过滤视图 = UIView()视图 = 过滤视图view.backgroundColor = #colorLiteral(红色:0.909803926944733,绿色:0.47843137383461,蓝色:0.643137276172638,alpha:1.0)滚动视图 = UIScrollView()scrollView.backgroundColor = #colorLiteral(红色:0.474509805440903,绿色:0.839215695858002,蓝色:0.976470589637756,alpha:1.0)view.addSubview(scrollView)scrollView.translatesAutoresizingMaskIntoConstraints = falsescrollView.topAnchor.constraint(equalTo:view.topAnchor, constant:40).isActive = truescrollView.leadingAnchor.constraint(equalTo:view.leadingAnchor).isActive = truescrollView.widthAnchor.constraint(equalTo:view.widthAnchor).isActive = truescrollView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.25).isActive = truescrollView.isScrollEnabled = true容器视图 = UIView()containerView.backgroundColor = #colorLiteral(红色:0.176470592617989,绿色:0.498039215803146,蓝色:0.756862759590149,alpha:1.0)scrollView.addSubview(containerView)containerView.translatesAutoresizingMaskIntoConstraints = falsecontainerView.topAnchor.constraint(equalTo:scrollView.topAnchor).isActive = truecontainerView.leadingAnchor.constraint(equalTo:scrollView.leadingAnchor).isActive = truecontainerView.trailingAnchor.constraint(equalTo:scrollView.trailingAnchor).isActive = truecontainerView.bottomAnchor.constraint(equalTo:scrollView.bottomAnchor).isActive = truecontainerView.heightAnchor.constraint(equalTo: scrollView.heightAnchor).isActive = true}类按钮{让按钮 = UIButton()初始化(标题文本:字符串){button.backgroundColor = #colorLiteral(红色:0.976470589637756,绿色:0.850980401039124,蓝色:0.549019634723663,alpha:1.0)button.setTitle(titleText, for: .normal)//button.frame = CGRect(x: 0, y: 0, width: 200, height: 200)}}覆盖 func viewDidLoad() {super.viewDidLoad()让 b1 = Buttons(titleText: "one")让 b2 = Buttons(titleText: "two")让 b3 = Buttons(titleText: "三")让 b4 = Buttons(titleText: "four")让 b5 = Buttons(titleText: "五")让 buttonArray = [b1,b2,b3,b4,b5]var startPoint = containerView.leadingAnchor对于按钮数组中的 btn {让 theBtn = btn.buttoncontainerView.addSubview(theBtn)theBtn.translatesAutoresizingMaskIntoConstraints = falsetheBtn.leadingAnchor.constraint(equalTo:startPoint, constant:20).isActive = truetheBtn.topAnchor.constraint(equalTo:containerView.topAnchor).isActive = truetheBtn.bottomAnchor.constraint(equalTo:containerView.bottomAnchor).isActive = truetheBtn.widthAnchor.constraint(equalTo: theBtn.heightAnchor).isActive = truestartPoint = theBtn.trailingAnchorcontainerView.widthAnchor.constraint(equalTo: theBtn.widthAnchor, multiplier:CGFloat(buttonArray.count), constant: CGFloat(buttonArray.count * 20)).isActive = true}}}让 filterViewController = FilterViewController()PlaygroundPage.current.liveView = filterViewController

解决方案

您可以使用 Auto Layout 来做到这一点.secret 是将 containerView 的边缘约束到 scrollView 的边缘.这不直观,但限制 containerView 的边缘并没有设置大小,它只是确保 scrollView 的内容大小随着 containerView 的增长而增长 增长.通过将 containerView 的宽度约束设置为大于 scrollView 宽度的常数,内容将水平滚动.

注意:这样配置scrollView时,不要设置scrollViewcontentSize.contentSize 将由 Auto Layout 为您计算,它将等于 containerView 的大小.确保 containerView 的大小完全由约束指定非常重要.

为了让它发挥作用,我做了以下更改:

containerView = UIView()containerView.backgroundColor = #colorLiteral(红色:0.176470592617989,绿色:0.498039215803146,蓝色:0.756862759590149,alpha:1.0)scrollView.addSubview(containerView)//containerView.frame = CGRect(x: 0, y: 0, width: 1080, height: 200)containerView.translatesAutoresizingMaskIntoConstraints = falsecontainerView.topAnchor.constraint(equalTo:scrollView.topAnchor).isActive = truecontainerView.leadingAnchor.constraint(equalTo:scrollView.leadingAnchor).isActive = truecontainerView.trailingAnchor.constraint(equalTo:scrollView.trailingAnchor).isActive = truecontainerView.bottomAnchor.constraint(equalTo:scrollView.bottomAnchor).isActive = truecontainerView.heightAnchor.constraint(equalToConstant: 200).isActive = truecontainerView.widthAnchor.constraint(equalToConstant: 1080).isActive = true

<小时><块引用>

为什么我的内容不滚动?

要使其滚动,containerView 必须大于scrollView.您的错误是您设置了约束,使得 containerView 的宽度和高度与 scrollView 相同,这就是您的内容不滚动的原因.

如果你想让它水平滚动,containerView的宽度必须大于scrollView的宽度.您可以通过以下两种方式之一执行此操作:

  1. containerView 指定一个显式的常量宽度,该宽度大于 scrollView 的宽度.

  2. containerView 的子视图从左到右连接起来,最左边的被限制在 containerView 的前沿.完全指定子视图的宽度,并在子视图之间放置距离约束.最右边的子视图必须与 containerView 的后沿有一个偏移量.通过这样做,Auto Layout 可以计算 containerView 的宽度并设置 scrollViewcontentSize.

<小时>

小项目:更新

这是您的迷你项目的一个版本,它使用一系列受约束的视图来定义 containerView 的宽度.关键是 viewDidLoad()for 循环之后的最终约束,它将最后一个按钮的 trailingAnchor(又名 startPoint)连接到 containerView 的 trailingAnchor.这样就完成了将 containerView 的前沿与 containerView 的后沿连接起来的约束和按钮链.有了这个,Auto Layout 能够计算出 containerView 的宽度,并确定 scrollViewcontentSize.

导入 UIKit导入游乐场支持类FilterViewController:UIViewController {var filterView: UIView!var 滚动视图:UIScrollView!var 容器视图:UIView!覆盖 func loadView() {过滤视图 = UIView()视图 = 过滤视图view.backgroundColor = #colorLiteral(红色:0.909803926944733,绿色:0.47843137383461,蓝色:0.643137276172638,alpha:1.0)滚动视图 = UIScrollView()scrollView.backgroundColor = #colorLiteral(红色:0.474509805440903,绿色:0.839215695858002,蓝色:0.976470589637756,alpha:1.0)view.addSubview(scrollView)scrollView.translatesAutoresizingMaskIntoConstraints = falsescrollView.topAnchor.constraint(equalTo: view.topAnchor, 常量: 40).isActive = truescrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = truescrollView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = truescrollView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.25).isActive = truescrollView.isScrollEnabled = true容器视图 = UIView()containerView.backgroundColor = #colorLiteral(红色:0.176470592617989,绿色:0.498039215803146,蓝色:0.756862759590149,alpha:1.0)scrollView.addSubview(containerView)containerView.translatesAutoresizingMaskIntoConstraints = false//这是关键:将 containerView 的所有四个边连接到//到scrollView的边缘containerView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = truecontainerView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = truecontainerView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor).isActive = truecontainerView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true//使 containerView 和 scrollView 高度相同意味着//内容不会垂直滚动containerView.heightAnchor.constraint(equalTo: scrollView.heightAnchor).isActive = true}类按钮 {让按钮 = UIButton()初始化(标题文本:字符串){button.backgroundColor = #colorLiteral(红色:0.976470589637756,绿色:0.850980401039124,蓝色:0.549019634723663,alpha:1.0)button.setTitle(titleText, for: .normal)}}覆盖 func viewDidLoad() {super.viewDidLoad()让 b1 = Buttons(titleText: "one")让 b2 = Buttons(titleText: "two")让 b3 = Buttons(titleText: "三")让 b4 = Buttons(titleText: "four")让 b5 = Buttons(titleText: "五")让 buttonArray = [b1, b2, b3, b4, b5]var startPoint = containerView.leadingAnchor对于按钮数组中的 btn {让 theBtn = btn.buttoncontainerView.addSubview(theBtn)theBtn.translatesAutoresizingMaskIntoConstraints = falsetheBtn.leadingAnchor.constraint(equalTo: startPoint, constant: 20).isActive = truetheBtn.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = truetheBtn.bottomAnchor.constraint(equalTo: containerView.bottomAnchor).isActive = truetheBtn.widthAnchor.constraint(equalTo: theBtn.heightAnchor).isActive = truestartPoint = theBtn.trailingAnchor}//完成约束链containerView.trailingAnchor.constraint(equalTo: startPoint, constant: 20).isActive = true}}让 filterViewController = FilterViewController()PlaygroundPage.current.liveView = filterViewController

I have created a small demo playground to get this working before adding the view to my app.

I have a scroll view that is going to contain a number of buttons to scroll through horizontally. I know that these buttons need to go into a container view within the scroll view and have created this as well. I was initially using autolayout constraints to create all of this, but have now tried using constants to ensure the content view is bigger than the scroll view. However, the buttons still will not scroll... have I missed something? Do scroll views not work with auto layout?

I am doing this all programmatically as well on my iPad so solutions with interface builder are unfortunately not an option...

Here is the full code:

import UIKit
import PlaygroundSupport

class FilterViewController: UIViewController {
    var filterView: UIView!
    var scrollView: UIScrollView!
    var containerView: UIView!

    override func loadView() {
        filterView = UIView()
        view = filterView
        view.backgroundColor = #colorLiteral(red: 0.909803926944733, green: 0.47843137383461, blue: 0.643137276172638, alpha: 1.0)

        scrollView = UIScrollView()
        scrollView.backgroundColor = #colorLiteral(red: 0.474509805440903, green: 0.839215695858002, blue: 0.976470589637756, alpha: 1.0)
        view.addSubview(scrollView)
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        scrollView.topAnchor.constraint(equalTo:view.topAnchor, constant:40).isActive = true
        scrollView.leadingAnchor.constraint(equalTo:view.leadingAnchor).isActive = true
        scrollView.widthAnchor.constraint(equalTo:view.widthAnchor).isActive = true
        scrollView.heightAnchor.constraint(equalToConstant: 200).isActive = true
        scrollView.isScrollEnabled = true

        containerView = UIView()
        containerView.backgroundColor = #colorLiteral(red: 0.176470592617989, green: 0.498039215803146, blue: 0.756862759590149, alpha: 1.0)
        scrollView.addSubview(containerView)
        containerView.frame = CGRect(x: 0, y: 0, width: 1080, height: 200)
    }

    class Buttons{
        let button = UIButton()
        init (titleText : String){
            button.backgroundColor = #colorLiteral(red: 0.976470589637756, green: 0.850980401039124, blue: 0.549019634723663, alpha: 1.0)
            button.setTitle(titleText, for: .normal)
            button.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let b1 = Buttons(titleText: "one")
        let b2 = Buttons(titleText: "two")
        let b3 = Buttons(titleText: "three")
        let b4 = Buttons(titleText: "four")
        let b5 = Buttons(titleText: "five")
        let buttonArray = [b1,b2,b3,b4,b5]
        var startPoint : CGFloat = 0.0
        for btn in buttonArray {
            let theBtn = btn.button
            containerView.addSubview(theBtn)
            theBtn.frame = CGRect(x: startPoint, y: 0, width: 200, height: 200)
            startPoint += 220
        }

    }
}

let filterViewController = FilterViewController()
PlaygroundPage.current.liveView = filterViewController

Thank you vacawama! Here is the full (working now) mini project with all of the auto layout constraints:

import UIKit
import PlaygroundSupport

class FilterViewController: UIViewController {
    var filterView: UIView!
    var scrollView: UIScrollView!
    var containerView: UIView!

    override func loadView() {
        filterView = UIView()
        view = filterView
        view.backgroundColor = #colorLiteral(red: 0.909803926944733, green: 0.47843137383461, blue: 0.643137276172638, alpha: 1.0)

        scrollView = UIScrollView()
        scrollView.backgroundColor = #colorLiteral(red: 0.474509805440903, green: 0.839215695858002, blue: 0.976470589637756, alpha: 1.0)
        view.addSubview(scrollView)
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        scrollView.topAnchor.constraint(equalTo:view.topAnchor, constant:40).isActive = true
        scrollView.leadingAnchor.constraint(equalTo:view.leadingAnchor).isActive = true
        scrollView.widthAnchor.constraint(equalTo:view.widthAnchor).isActive = true
        scrollView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.25).isActive = true
        scrollView.isScrollEnabled = true

        containerView = UIView()
        containerView.backgroundColor = #colorLiteral(red: 0.176470592617989, green: 0.498039215803146, blue: 0.756862759590149, alpha: 1.0)
        scrollView.addSubview(containerView)
        containerView.translatesAutoresizingMaskIntoConstraints = false
        containerView.topAnchor.constraint(equalTo:scrollView.topAnchor).isActive = true
        containerView.leadingAnchor.constraint(equalTo:scrollView.leadingAnchor).isActive = true
        containerView.trailingAnchor.constraint(equalTo:scrollView.trailingAnchor).isActive = true
        containerView.bottomAnchor.constraint(equalTo:scrollView.bottomAnchor).isActive = true
        containerView.heightAnchor.constraint(equalTo: scrollView.heightAnchor).isActive = true



    }

    class Buttons{
        let button = UIButton()
        init (titleText : String){
            button.backgroundColor = #colorLiteral(red: 0.976470589637756, green: 0.850980401039124, blue: 0.549019634723663, alpha: 1.0)
            button.setTitle(titleText, for: .normal)
            //button.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let b1 = Buttons(titleText: "one")
        let b2 = Buttons(titleText: "two")
        let b3 = Buttons(titleText: "three")
        let b4 = Buttons(titleText: "four")
        let b5 = Buttons(titleText: "five")
        let buttonArray = [b1,b2,b3,b4,b5]
        var startPoint = containerView.leadingAnchor
        for btn in buttonArray {
            let theBtn = btn.button
            containerView.addSubview(theBtn)
            theBtn.translatesAutoresizingMaskIntoConstraints = false
            theBtn.leadingAnchor.constraint(equalTo:startPoint, constant:20).isActive = true
            theBtn.topAnchor.constraint(equalTo:containerView.topAnchor).isActive = true
            theBtn.bottomAnchor.constraint(equalTo:containerView.bottomAnchor).isActive = true
            theBtn.widthAnchor.constraint(equalTo: theBtn.heightAnchor).isActive = true
            startPoint = theBtn.trailingAnchor
            containerView.widthAnchor.constraint(equalTo: theBtn.widthAnchor, multiplier:CGFloat(buttonArray.count), constant: CGFloat(buttonArray.count * 20)).isActive = true
        }
    }
}

let filterViewController = FilterViewController()
PlaygroundPage.current.liveView = filterViewController

解决方案

You can do this with Auto Layout. The secret is to constrain the edges of the containerView to the edges of the scrollView. It's not intuitive, but constraining the edges of the containerView doesn't set the size, it just makes sure that the content size of the scrollView grows as the containerView grows. By setting constraints for the width of the containerView to a constant that is a larger number than the width of the scrollView, the content will scroll horizontally.

Note: When configuring a scrollView this way, you do not set the contentSize of the scrollView. The contentSize will be computed for you by Auto Layout and it will be equal to the size of the containerView. It is important to make sure that the size of the containerView is fully specified by the constraints.

Here's what I changed to make it work:

containerView = UIView()
containerView.backgroundColor = #colorLiteral(red: 0.176470592617989, green: 0.498039215803146, blue: 0.756862759590149, alpha: 1.0)
scrollView.addSubview(containerView)
//containerView.frame = CGRect(x: 0, y: 0, width: 1080, height: 200)
containerView.translatesAutoresizingMaskIntoConstraints = false
containerView.topAnchor.constraint(equalTo:scrollView.topAnchor).isActive = true
containerView.leadingAnchor.constraint(equalTo:scrollView.leadingAnchor).isActive = true
containerView.trailingAnchor.constraint(equalTo:scrollView.trailingAnchor).isActive = true
containerView.bottomAnchor.constraint(equalTo:scrollView.bottomAnchor).isActive = true
containerView.heightAnchor.constraint(equalToConstant: 200).isActive = true
containerView.widthAnchor.constraint(equalToConstant: 1080).isActive = true


Why isn't my content scrolling?

For it to scroll, the containerView must be larger than the scrollView. Your error is that you have set the constraints such that the containerView is the same width and height as the scrollView, and that is why your content isn't scrolling.

If you want it to scroll horizontally, the width of the containerView must be larger than the scrollView's width. You can do this in one of two ways:

  1. Specify an explicit constant width for the containerView that is larger than the scrollView's width.

    OR

  2. Chain the subviews of the containerView from left to right with the left most being constained to the leading edge of the containerView. Fully specify the widths of the subviews, and place distance contraints between the subviews. The rightmost subview must have an offset from the trailing edge of the containerView. By doing this, Auto Layout can compute the width of the containerView and set the contentSize of the scrollView.


Mini project: update

This is a version of your mini project which uses a chain of constrained views to define the containerView's width. The key is the final constraint after the for loop in viewDidLoad() which connects the last button's trailingAnchor (aka startPoint) to the containerView's trailingAnchor. This completes the chain of contraints and buttons which connect the leading edge of the containerView with the trailing edge of containerView. With this, Auto Layout is able to compute the width of the containerView and establish the contentSize of the scrollView.

import UIKit
import PlaygroundSupport

class FilterViewController: UIViewController {
    var filterView: UIView!
    var scrollView: UIScrollView!
    var containerView: UIView!

    override func loadView() {
        filterView = UIView()
        view = filterView
        view.backgroundColor = #colorLiteral(red: 0.909803926944733, green: 0.47843137383461, blue: 0.643137276172638, alpha: 1.0)

        scrollView = UIScrollView()
        scrollView.backgroundColor = #colorLiteral(red: 0.474509805440903, green: 0.839215695858002, blue: 0.976470589637756, alpha: 1.0)
        view.addSubview(scrollView)
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        scrollView.topAnchor.constraint(equalTo: view.topAnchor, constant: 40).isActive = true
        scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        scrollView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
        scrollView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.25).isActive = true
        scrollView.isScrollEnabled = true

        containerView = UIView()
        containerView.backgroundColor = #colorLiteral(red: 0.176470592617989, green: 0.498039215803146, blue: 0.756862759590149, alpha: 1.0)
        scrollView.addSubview(containerView)
        containerView.translatesAutoresizingMaskIntoConstraints = false

        // This is key:  connect all four edges of the containerView to
        // to the edges of the scrollView
        containerView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
        containerView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true
        containerView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor).isActive = true
        containerView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true

        // Making containerView and scrollView the same height means the
        // content will not scroll vertically
        containerView.heightAnchor.constraint(equalTo: scrollView.heightAnchor).isActive = true    
    }

    class Buttons {
        let button = UIButton()
        init(titleText: String) {
            button.backgroundColor = #colorLiteral(red: 0.976470589637756, green: 0.850980401039124, blue: 0.549019634723663, alpha: 1.0)
            button.setTitle(titleText, for: .normal)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let b1 = Buttons(titleText: "one")
        let b2 = Buttons(titleText: "two")
        let b3 = Buttons(titleText: "three")
        let b4 = Buttons(titleText: "four")
        let b5 = Buttons(titleText: "five")
        let buttonArray = [b1, b2, b3, b4, b5]
        var startPoint = containerView.leadingAnchor
        for btn in buttonArray {
            let theBtn = btn.button
            containerView.addSubview(theBtn)
            theBtn.translatesAutoresizingMaskIntoConstraints = false
            theBtn.leadingAnchor.constraint(equalTo: startPoint, constant: 20).isActive = true
            theBtn.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
            theBtn.bottomAnchor.constraint(equalTo: containerView.bottomAnchor).isActive = true
            theBtn.widthAnchor.constraint(equalTo: theBtn.heightAnchor).isActive = true
            startPoint = theBtn.trailingAnchor
        }
        // Complete the chain of constraints
        containerView.trailingAnchor.constraint(equalTo: startPoint, constant: 20).isActive = true
    }
}

let filterViewController = FilterViewController()
PlaygroundPage.current.liveView = filterViewController

这篇关于我的带有自动布局约束的 Swift 4 UIScrollView 没有滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一条:每次释放 UIScrollView 时内存泄漏 下一条:滚动视图不起作用 IOS 7

相关文章

最新文章