1

我如何使用 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)
        }
    }
4

1 回答 1

0

实现等距按钮数组的最佳方法是使用 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()
}
于 2017-03-07T18:40:37.367 回答