ios - 设置 button.frame.origin 不会快速更改 scrollView 中的位置
问题描述
我正在快速以编程方式在垂直 ScrollView 中添加一些按钮。对于每个按钮,我计算同一行中的房间。如果房间足够,则在上一个按钮之后添加按钮,否则将添加到新行。我尝试使用设置每个按钮的位置
topButtons[j].frame.origin.x = currentOriginX
topButtons[j].frame.origin.y = currentOriginY
其中 currentOriginX 和 currentOriginY 在每次迭代中都会发生变化。但是,按钮仍然出现在滚动视图的左上角,因此彼此重叠。我该如何解决这个问题?(另外我想保持按钮宽度与其文本相同,所以我没有设置按钮的宽度。)
我的代码附在下面。
@IBOutlet weak var topScrollView: UIScrollView!
var topButtons = [UIButtons()]
func displayTopItems() {
let scrollViewWidth = topScrollView.frame.size.width
let buttonHeight = CGFloat(40)
var currentOriginX = topScrollView.frame.origin.x
var currentOriginY = topScrollView.frame.origin.y
self.topScrollView.automaticallyAdjustsScrollIndicatorInsets = false
self.topScrollView.isScrollEnabled = true
self.topScrollView.isUserInteractionEnabled = true
self.topScrollView.translatesAutoresizingMaskIntoConstraints = false
for j in 0..<self.itemsClass.toBuyItems.count {
topButtons.append(UIButton(type: UIButton.ButtonType.system))
topButtons[j].setTitle(self.itemsClass.toBuyItems[j], for: UIControl.State.normal)
let buttonWidth = topButtons[j].frame.size.width
if j > 0 {
let lastButtonLeft = topButtons[j-1].frame.origin.x
let lastButtonWidth = topButtons[j-1].frame.size.width
let sameRow = lastButtonLeft + lastButtonWidth + buttonWidth + 50 < scrollViewWidth
if sameRow {
currentOriginX += lastButtonWidth + 30
}
else {
currentOriginX = 0
currentOriginY += buttonSize.height + 20
}
}
// self.topScrollView.contentOffset.x = currentOriginX
// self.topScrollView.contentOffset.y = currentOriginY
topButtons[j].frame.origin.x = currentOriginX
topButtons[j].frame.origin.y = currentOriginY
topButtons[j].setTitleColor(UIColor.white, for: UIControl.State.normal)
topButtons[j].backgroundColor = UIColor.black
topButtons[j].translatesAutoresizingMaskIntoConstraints = false
topButtons[j].heightAnchor.constraint(equalToConstant: buttonSize.height).isActive = true
topButtons[j].layer.borderColor = UIColor.red.cgColor
topButtons[j].layer.cornerRadius = 10.0
topButtons[j].addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
topButtons[j].tag = j
self.topScrollView.addSubview(topButtons[j])
}
}
解决方案
按钮的位置需要在添加到视图后通过指定约束来设置。
topButtons[j].setTitleColor(UIColor.white, for: UIControl.State.normal)
topButtons[j].backgroundColor = UIColor.black
topButtons[j].translatesAutoresizingMaskIntoConstraints = false
topButtons[j].heightAnchor.constraint(equalToConstant: buttonSize.height).isActive = true
topButtons[j].layer.borderColor = UIColor.red.cgColor
topButtons[j].layer.cornerRadius = 10.0
topButtons[j].addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
topButtons[j].tag = j
self.topScrollView.addSubview(topButtons[j])
// do this after adding buttons to view
topButtons[j].topAnchor.constraint(equalTo: topScrollView.topAnchor, constant: currentOriginY).isActive = true
topButtons[j].leadingAnchor.constraint(equalTo: topScrollView.leadingAnchor, constant: currentOriginX).isActive = true