Swift 如何使用SnapKit将ScrollView中的按钮阵列居中
如何使用SnapKit在ScrollView中垂直居中显示下面的按钮阵列Swift 如何使用SnapKit将ScrollView中的按钮阵列居中,swift,snapkit,Swift,Snapkit,如何使用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.backgroundC
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)
}
}
对于i in 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()
}
let buttonStackView=UIStackView()
buttonStackView.axis=.vertical
buttonStackView.distribution=.equalSpacing
对于0..中的i,实现等间距按钮阵列的最佳方法是使用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()
}
let buttonStackView=UIStackView()
buttonStackView.axis=.vertical
buttonStackView.distribution=.equalSpacing
对于0中的i..更改为水平并得到此结果,我猜按钮之间的间隙与stackview冲突。distribution@mclovin将stackview.spacing=gapBetweenButtons更改为水平,我想gapBetweenButtons与stackview冲突。distribution@mclovin设置stackview.spacing=GapBetween按钮