我有一个字符串数组,对于每个字符串值,我想创建一个UILabel来显示文本,这意味着如果数组中有10个字符串,我想有10个带有字符串名称的标签。我正在执行此programmaticalyy,但由于此方法无效,因此未显示任何内容。这就是我尝试过的
syncing
然后在viewDidLoad中调用layout()
答案 0 :(得分:2)
您可以尝试
没有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)
}
}
}