我想知道是否有人可以通过编程限制来帮助我。我想要的是:
我在循环内创建按钮,按钮水平对齐。某些按钮具有不同的宽度,因此这意味着按钮可以与另一个按钮重叠。是否可以使用约束来防止重叠并在其间添加空格?
提前致谢
代码:
for (j, _) in buttonInfo.enumerated(){
let button: UIButton = buttonArray[j]
let stackView = UIStackView();
stackView.spacing = 30;
button.tag = Int((buttonInfo[j]?.pID)!)!
button.setTitle(buttonInfo[j]?.title, for: .normal)
button.addTarget(self, action: #selector(PalletViewController.productCall), for: .touchUpInside)
let stock = Int(buttonInfo[j]!.stock)
if(stock! == 0 || stock! < 0){
button.isHidden = true;
}
let views = ["view": view, "button": button] as [String : Any]
let spacing = NSLayoutConstraint.constraints(withVisualFormat: "H:|-(10)-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views);
view.addConstraints(spacing);
}
答案 0 :(得分:1)