[英]creating UI components based on array values swift
我有一個字符串數組,對於每個字符串值,我想創建一個UILabel來顯示文本,這意味着如果數組中有10個字符串,我想有10個帶有字符串名稱的標簽。 我正在執行此programmaticalyy,但由於此方法無效,因此未顯示任何內容。 這就是我嘗試過的
let const: [String] = ["Central Cooling Unit", "Split Cooling Unit", "Oven", "Refridgerator", "Dish Washer"]
var checkBox: BEMCheckBox!
var checkLabel: DefaultLabel!
var checkboxStack: DefaultStackView?
fileprivate func setupButton() {
const.forEach { (title) in
checkBox = BEMCheckBox()
checkBox.translatesAutoresizingMaskIntoConstraints = false
checkBox.tintColor = UIColor.ZAMA.offWhite
checkBox.onCheckColor = UIColor.ZAMA.primary
checkBox.onFillColor = UIColor.ZAMA.tabColor
checkBox.onTintColor = UIColor.ZAMA.primary
checkBox.onAnimationType = .flat
checkBox.heightAnchor.constraint(equalToConstant: 25).isActive = true
checkBox.widthAnchor.constraint(equalToConstant: 25).isActive = true
checkLabel = UILabel()
checkLabel.text = title
checkLabel.textColor = .black
checkLabel.fontSize = 15
checkboxStack = UIStackView(arrangedSubviews: [centralCoolingCheckbox, centralCoolingText])
checkboxStack?.axis = .horizontal
checkboxStack?.spacing = 4
checkboxStack?.alignment = .fill
checkboxStack?.distribution = .fill
}
}
fileprivate func layout() {
view.addSubview(scrollView)
hideKeyboardWhenTappedAround()
setupButton()
stack = UIStackView(arrangedSubviews: [checkboxStack ?? DefaultStackView()])
stack.axis = .vertical
stack.distribution = .fillEqually
stack.spacing = 8
view.addSubview(stack)
stack.anchor(top: contentView.topAnchor, left: contentView.leftAnchor, bottom: nil, right: contentView.rightAnchor, paddingTop: 4, paddingLeft: 8, paddingBottom: 0, paddingRight: 8, width: 0, height: 0, enableInsets: false)
}
然后在viewDidLoad中調用layout()
你可以嘗試像
沒有ScrollView
class ViewController: UIViewController {
let const = ["Central Cooling Unit", "Split Cooling Unit", "Oven", "Refridgerator", "Dish Washer","Central Cooling Unit", "Split Cooling Unit", "Oven", "Refridgerator", "Dish Washer","Central Cooling Unit", "Split Cooling Unit", "Oven", "Refridgerator", "Dish Washer"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let st = UIStackView()
st.axis = .vertical
st.alignment = .center
st.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(st)
NSLayoutConstraint.activate([
st.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
st.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
st.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor)
])
const.forEach {
let lbl = UILabel()
lbl.text = $0
st.addArrangedSubview(lbl)
}
}
}
使用ScrollView
class ViewController: UIViewController {
let const = ["Central Cooling Unit", "Split Cooling Unit", "Oven", "Refridgerator", "Dish Washer","Central Cooling Unit", "Split Cooling Unit", "Oven", "Refridgerator", "Dish Washer","Central Cooling Unit", "Split Cooling Unit", "Oven", "Refridgerator", "Dish Washer","Central Cooling Unit", "Split Cooling Unit", "Oven", "Refridgerator", "Dish Washer","Central Cooling Unit", "Split Cooling Unit", "Oven", "Refridgerator", "Dish Washer","Central Cooling Unit", "Split Cooling Unit", "Oven", "Refridgerator", "Dish Washer","Central Cooling Unit", "Split Cooling Unit", "Oven", "Refridgerator", "Dish Washer","Central Cooling Unit", "Split Cooling Unit", "Oven", "Refridgerator", "Dish Washer","Central Cooling Unit", "Split Cooling Unit", "Oven", "Refridgerator", "Dish Washer","Central Cooling Unit", "Split Cooling Unit", "Oven", "Refridgerator", "Dish Washer","Central Cooling Unit", "Split Cooling Unit", "Oven", "Refridgerator", "Dish Washer","Central Cooling Unit", "Split Cooling Unit", "Oven", "Refridgerator", "Dish Washer"]
let scroll = UIScrollView()
let st = UIStackView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
st.axis = .vertical
st.alignment = .center
scroll.translatesAutoresizingMaskIntoConstraints = false
st.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(scroll)
scroll.addSubview(st)
NSLayoutConstraint.activate([
scroll.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
scroll.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
scroll.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
scroll.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
st.widthAnchor.constraint(equalTo: self.view.widthAnchor),
st.leadingAnchor.constraint(equalTo: self.scroll.leadingAnchor),
st.trailingAnchor.constraint(equalTo: self.scroll.trailingAnchor),
st.topAnchor.constraint(equalTo: self.scroll.topAnchor),
st.bottomAnchor.constraint(equalTo: self.scroll.bottomAnchor)
])
const.forEach {
let lbl = UILabel()
lbl.text = $0
st.addArrangedSubview(lbl)
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.