【问题标题】:How do i center array of button in ScrollView using SnapKit如何使用 SnapKit 在 ScrollView 中居中按钮数组
【发布时间】:2017-03-07 08:35:34
【问题描述】:

如何使用 SnapKit 在 ScrollView 中垂直居中下方的按钮数组。

for i in 0..<3 {

        itemCount = i

        let aButton = UIButton(type: .custom)

        aButton.frame = CGRect(x: xCoord, y: yCoord, width: buttonWidth, height: buttonHeight)
        aButton.backgroundColor = UIColor.blue
        aButton.layer.cornerRadius = aButton.frame.size.width / 2
        aButton.clipsToBounds = true

        xCoord += buttonWidth + gapBetweenButtons

        aScrollView.addSubview(aButton)

        aButton.snp.makeConstraints { (make) in
            make.center.equalTo(aScrollView)
        }
    }

【问题讨论】:

    标签: swift snapkit


    【解决方案1】:

    实现等距按钮数组的最佳方法是使用 UIStackView。这应该有效:

    let buttonStackView = UIStackView()
    buttonStackView.axis = .vertical
    buttonStackView.distribution = .equalSpacing
    for i in 0..<3 {
        itemCount = i
    
        let aButton = UIButton(type: .custom)
    
        aButton.frame = CGRect(x: xCoord, y: yCoord, width: buttonWidth, height: buttonHeight)
        aButton.backgroundColor = UIColor.blue
        aButton.layer.cornerRadius = aButton.frame.size.width / 2
        aButton.clipsToBounds = true
    
        xCoord += buttonWidth + gapBetweenButtons
    
        buttonStackView.addArrangedSubview(aButton)
    
    }
    aScrollView.addSubview(buttonStackView)
    buttonStackView.snp.makeConstraints { (make) in
        make.centerY.equalToSuperview()
    }
    

    【讨论】:

    • 更改为水平并得到这个imgur.com/a/q29pB,我猜 gapBetweenButtons 与 stackview.distribution 冲突
    • @mclovin 设置 stackview.spacing = gapBetweenButtons
    猜你喜欢
    • 2021-02-27
    • 1970-01-01
    • 2021-09-09
    • 2011-11-25
    • 1970-01-01
    • 2013-09-13
    • 2021-05-13
    • 2019-09-11
    相关资源
    最近更新 更多