pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

我的帶有自動布局約束的 Swift 4 UIScrollView 沒有滾

My Swift 4 UIScrollView with autolayout constraints is not scrolling(我的帶有自動布局約束的 Swift 4 UIScrollView 沒有滾動)
本文介紹了我的帶有自動布局約束的 Swift 4 UIScrollView 沒有滾動的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

在將視圖添加到我的應(yīng)用程序之前,我創(chuàng)建了一個小型演示游樂場以使其正常工作.

我有一個滾動視圖,其中包含許多用于水平滾動的按鈕.我知道這些按鈕需要進(jìn)入滾動視圖中的容器視圖,并且也創(chuàng)建了它.我最初使用自動布局約束來創(chuàng)建所有這些,但現(xiàn)在嘗試使用常量來確保內(nèi)容視圖大于滾動視圖.但是,按鈕仍然不會滾動......我錯過了什么嗎?滾動視圖是否不適用于自動布局?

我也在我的 iPad 上以編程方式完成這一切,所以很遺憾,界面生成器的解決方案不是一個選項......

這里是完整的代碼:

導(dǎo)入 UIKit導(dǎo)入游樂場支持類FilterViewController:UIViewController {var filterView: UIView!var 滾動視圖:UIScrollView!var 容器視圖:UIView!覆蓋 func loadView() {過濾視圖 = UIView()視圖 = 過濾視圖view.backgroundColor = #colorLiteral(紅色:0.909803926944733,綠色:0.47843137383461,藍(lán)色:0.643137276172638,alpha:1.0)滾動視圖 = UIScrollView()scrollView.backgroundColor = #colorLiteral(紅色:0.474509805440903,綠色:0.839215695858002,藍(lán)色: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,藍(lán)色:0.756862759590149,alpha:1.0)scrollView.addSubview(containerView)containerView.frame = CGRect(x: 0, y: 0, width: 1080, height: 200)}類按鈕{讓按鈕 = UIButton()初始化(標(biāo)題文本:字符串){button.backgroundColor = #colorLiteral(紅色:0.976470589637756,綠色:0.850980401039124,藍(lán)色: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對于按鈕數(shù)組中的 btn {讓 theBtn = btn.buttoncontainerView.addSubview(theBtn)theBtn.frame = CGRect(x: startPoint, y: 0, width: 200, height: 200)起點 += 220}}}讓 filterViewController = FilterViewController()PlaygroundPage.current.liveView = filterViewController

謝謝你!這是包含所有自動布局約束的完整(正在運行)迷你項目:

導(dǎo)入 UIKit導(dǎo)入游樂場支持類FilterViewController:UIViewController {var filterView: UIView!var 滾動視圖:UIScrollView!var 容器視圖:UIView!覆蓋 func loadView() {過濾視圖 = UIView()視圖 = 過濾視圖view.backgroundColor = #colorLiteral(紅色:0.909803926944733,綠色:0.47843137383461,藍(lán)色:0.643137276172638,alpha:1.0)滾動視圖 = UIScrollView()scrollView.backgroundColor = #colorLiteral(紅色:0.474509805440903,綠色:0.839215695858002,藍(lán)色: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,藍(lán)色: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()初始化(標(biāo)題文本:字符串){button.backgroundColor = #colorLiteral(紅色:0.976470589637756,綠色:0.850980401039124,藍(lán)色: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對于按鈕數(shù)組中的 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 的邊緣并沒有設(shè)置大小,它只是確保 scrollView 的內(nèi)容大小隨著 containerView 的增長而增長 增長.通過將 containerView 的寬度約束設(shè)置為大于 scrollView 寬度的常數(shù),內(nèi)容將水平滾動.

注意:這樣配置scrollView時,不要設(shè)置scrollViewcontentSize.contentSize 將由 Auto Layout 為您計算,它將等于 containerView 的大小.確保 containerView 的大小完全由約束指定非常重要.

為了讓它發(fā)揮作用,我做了以下更改:

containerView = UIView()containerView.backgroundColor = #colorLiteral(紅色:0.176470592617989,綠色:0.498039215803146,藍(lán)色: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

<小時><塊引用>

為什么我的內(nèi)容不滾動?

要使其滾動,containerView 必須大于scrollView.您的錯誤是您設(shè)置了約束,使得 containerView 的寬度和高度與 scrollView 相同,這就是您的內(nèi)容不滾動的原因.

如果你想讓它水平滾動,containerView的寬度必須大于scrollView的寬度.您可以通過以下兩種方式之一執(zhí)行此操作:

  1. containerView 指定一個顯式的常量寬度,該寬度大于 scrollView 的寬度.

  2. containerView 的子視圖從左到右連接起來,最左邊的被限制在 containerView 的前沿.完全指定子視圖的寬度,并在子視圖之間放置距離約束.最右邊的子視圖必須與 containerView 的后沿有一個偏移量.通過這樣做,Auto Layout 可以計算 containerView 的寬度并設(shè)置 scrollViewcontentSize.

<小時>

小項目:更新

這是您的迷你項目的一個版本,它使用一系列受約束的視圖來定義 containerView 的寬度.關(guān)鍵是 viewDidLoad()for 循環(huán)之后的最終約束,它將最后一個按鈕的 trailingAnchor(又名 startPoint)連接到 containerView 的 trailingAnchor.這樣就完成了將 containerView 的前沿與 containerView 的后沿連接起來的約束和按鈕鏈.有了這個,Auto Layout 能夠計算出 containerView 的寬度,并確定 scrollViewcontentSize.

導(dǎo)入 UIKit導(dǎo)入游樂場支持類FilterViewController:UIViewController {var filterView: UIView!var 滾動視圖:UIScrollView!var 容器視圖:UIView!覆蓋 func loadView() {過濾視圖 = UIView()視圖 = 過濾視圖view.backgroundColor = #colorLiteral(紅色:0.909803926944733,綠色:0.47843137383461,藍(lán)色:0.643137276172638,alpha:1.0)滾動視圖 = UIScrollView()scrollView.backgroundColor = #colorLiteral(紅色:0.474509805440903,綠色:0.839215695858002,藍(lán)色: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,藍(lán)色:0.756862759590149,alpha:1.0)scrollView.addSubview(containerView)containerView.translatesAutoresizingMaskIntoConstraints = false//這是關(guān)鍵:將 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 高度相同意味著//內(nèi)容不會垂直滾動containerView.heightAnchor.constraint(equalTo: scrollView.heightAnchor).isActive = true}類按鈕 {讓按鈕 = UIButton()初始化(標(biāo)題文本:字符串){button.backgroundColor = #colorLiteral(紅色:0.976470589637756,綠色:0.850980401039124,藍(lán)色: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對于按鈕數(shù)組中的 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

這篇關(guān)于我的帶有自動布局約束的 Swift 4 UIScrollView 沒有滾動的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to subclass UIScrollView and make the delegate property private(如何繼承 UIScrollView 并使委托屬性私有)
Swift - how to get last taken 3 photos from photo library?(Swift - 如何從照片庫中獲取最后拍攝的 3 張照片?)
Setting contentOffset programmatically triggers scrollViewDidScroll(以編程方式設(shè)置 contentOffset 觸發(fā) scrollViewDidScroll)
Photos app-like gap between pages in UIScrollView with pagingEnabled(使用 pagingEnabled 的 UIScrollView 中頁面之間的照片應(yīng)用程序式間隙)
why UIScrollView is leaving space from top in ios 6 and ios 7(為什么 UIScrollView 在 ios 6 和 ios 7 中從頂部留下空間)
UIScrollView pauses NSTimer while scrolling(UIScrollView 在滾動時暫停 NSTimer)
主站蜘蛛池模板: 滤芯,过滤器,滤油机,贺德克滤芯,精密滤芯_新乡市宇清流体净化技术有限公司 | 环氧树脂地坪漆_济宁市新天地漆业有限公司 | 青岛成人高考_山东成考报名网| 高铝轻质保温砖_刚玉莫来石砖厂家_轻质耐火砖价格 | 二氧化碳/活性炭投加系统,次氯酸钠发生器,紫外线消毒设备|广州新奥 | 深圳APP开发公司_软件APP定制开发/外包制作-红匣子科技 | 强效碱性清洗剂-实验室中性清洗剂-食品级高纯氮气发生器-上海润榕科学器材有限公司 | 书信之家_书信标准模板范文大全 最新范文网_实用的精品范文美文网 | 广州中央空调回收,二手中央空调回收,旧空调回收,制冷设备回收,冷气机组回收公司-广州益夫制冷设备回收公司 | 铆钉机|旋铆机|东莞旋铆机厂家|鸿佰专业生产气压/油压/自动铆钉机 | 西安文都考研官网_西安考研辅导班_考研培训机构_西安在职考研培训 | 喷码机,激光喷码打码机,鸡蛋打码机,手持打码机,自动喷码机,一物一码防伪溯源-恒欣瑞达有限公司 | 撕碎机_轮胎破碎机_粉碎机_回收生产线厂家_东莞华达机械有限公司 | 哲力实业_专注汽车涂料汽车漆研发生产_汽车漆|修补油漆品牌厂家 长沙一级消防工程公司_智能化弱电_机电安装_亮化工程专业施工承包_湖南公共安全工程有限公司 | 气动隔膜阀_气动隔膜阀厂家_卫生级隔膜阀价格_浙江浙控阀门有限公司 | 北京森语科技有限公司-模型制作专家-展览展示-沙盘模型设计制作-多媒体模型软硬件开发-三维地理信息交互沙盘 | 茶楼装修设计_茶馆室内设计效果图_云臻轩茶楼装饰公司 | 轻型地埋电缆故障测试仪,频响法绕组变形测试仪,静荷式卧式拉力试验机-扬州苏电 | 沥青灌缝机_路面灌缝机_道路灌缝机_沥青灌缝机厂家_济宁萨奥机械有限公司 | 防火阀、排烟防火阀、电动防火阀产品生产销售商-德州凯亿空调设备有限公司 | 中天寰创-内蒙古钢结构厂家|门式刚架|钢结构桁架|钢结构框架|包头钢结构煤棚 | 便携式高压氧舱-微压氧舱-核生化洗消系统-公众洗消站-洗消帐篷-北京利盟救援 | 温泉机设备|温泉小镇规划设计|碳酸泉设备 - 大连连邦温泉科技 | 湖南印刷厂|长沙印刷公司|画册印刷|挂历印刷|台历印刷|杂志印刷-乐成印刷 | 乐泰胶水_loctite_乐泰胶_汉高乐泰授权(中国)总代理-鑫华良供应链 | 不锈钢电动球阀_气动高压闸阀_旋塞疏水调节阀_全立阀门-来自温州工业阀门巨头企业 | 重庆小面培训_重庆小面技术培训学习班哪家好【终身免费复学】 | 考试试题_试卷及答案_诗词单词成语 - 优易学 | 重庆磨床过滤机,重庆纸带过滤机,机床伸缩钣金,重庆机床钣金护罩-重庆达鸿兴精密机械制造有限公司 | 脱硝喷枪-氨水喷枪-尿素喷枪-河北思凯淋环保科技有限公司 | 雷冲击高压发生器-水内冷直流高压发生器-串联谐振分压器-武汉特高压电力科技有限公司 | 亮化工程,亮化设计,城市亮化工程,亮化资质合作,长沙亮化照明,杰奥思【官网】 | 达利园物流科技集团-| 全温恒温摇床-水浴气浴恒温摇床-光照恒温培养摇床-常州金坛精达仪器制造有限公司 | 大数据营销公司_舆情监测软件_上海SEO公司-文军营销官网 | pos机办理,智能/扫码/二维码/微信支付宝pos机-北京万汇通宝商贸有限公司 | 脉冲除尘器,除尘器厂家-淄博机械 | 爱德华真空泵油/罗茨泵维修,爱发科-比其尔产品供应东莞/杭州/上海等全国各地 | 不锈钢监控杆_监控立杆厂家-廊坊耀星光电科技有限公司 | 氟氨基酮、氯硝柳胺、2-氟苯甲酸、异香兰素-新晨化工 | 聚合氯化铝厂家-聚合氯化铝铁价格-河南洁康环保科技 |