gpt4 book ai didi

swift - 我如何使用 SnapKit 在 ScrollView 中居中按钮数组

转载 作者:行者123 更新时间:2023-11-28 08:15:30 29 4
gpt4 key购买 nike

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

最佳答案

实现等间距按钮数组的最佳方法是使用 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()
}

关于swift - 我如何使用 SnapKit 在 ScrollView 中居中按钮数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42643592/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com