gpt4 book ai didi

swift - 添加多个 UI 元素并一起约束

转载 作者:行者123 更新时间:2023-11-28 07:38:11 25 4
gpt4 key购买 nike

我有一个像这样的 UIView 设置

  • 界面 View
    • UIScrollView (self.scrollView)
      • UIView (self.contentView)
      • LineChartView (self.chart)
      • M13CheckBox [UIControl 的子集]

图表显示包括多条线,我打算用多个复选框来控制它们。但是,我似乎无法将复选框相互约束!

这是我的代码:

    var unconstrainedChecks = [M13Checkbox]()
for year in years { // years = ["2015","2016","2017"]
let checkbox = CustomizedCheckBox().checkbox
checkbox.backgroundColor = .purple
self.contentView.addSubview(checkbox)
unconstrainedChecks.append(checkbox)
}
for i in 0..<unconstrainedChecks.count - 1 {
print(i)
let checkbox = unconstrainedChecks[i]
let nextCheckbox = unconstrainedChecks[i+1]
checkbox.snp.makeConstraints { (make) in
if i == 0 {
make.top.equalTo(self.chart.snp.bottom).offset(20)
make.width.height.equalTo(50)
make.left.equalTo(self.contentView)
}
make.bottom.equalTo(nextCheckbox.snp.top)
}

nextCheckbox.snp.makeConstraints { (make) in
make.width.height.equalTo(50)
make.left.equalTo(self.contentView)
make.top.equalTo(checkbox.snp.bottom)
}

}

CustomizedCheckBox 在哪里

class CustomizedCheckBox {
let checkbox: M13Checkbox
init() {
checkbox = M13Checkbox()
checkbox.setCheckState(.checked, animated: false)
checkbox.stateChangeAnimation = .bounce(.fill)
checkbox.secondaryTintColor = UIColor(hexString: "47cae8")
checkbox.secondaryCheckmarkTintColor = .white //checkmark
checkbox.tintColor = UIColor(hexString: "53cce7")
}
}

我查看了 View Hierarchal Thing,但根本看不到复选框。

最佳答案

我解决了这个问题。我需要将最后一个复选框限制为 self.contentView.bottom

将其添加到 nextCheckbox 循环中。

            if i + 1 == unconstrainedChecks.count - 1 {
make.bottom.equalTo(self.contentView.snp.bottom)
}

关于swift - 添加多个 UI 元素并一起约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52884289/

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