我編程方式創建我的用戶界面,這是我如何創建一個按鈕創建動態多的UIButton編程
let button: UIButton = {
let button = UIButton()
button.setTitle("Try1",.normal)
return button
}
而這正是我想做的事情。例如movie1有2個類別,Dram &恐怖我只想顯示其中2個。 Movie2有4個類別,Dram &恐怖&運動&戰爭我只是想顯示其中4個。
現在我堅持我不知道該怎麼做。我知道你們會給負代表,但我需要幫助。
完成!溶液低於
首先我已經作出了結構
struct Category {
let title: String
let color: UIColor
init(title: String, color: UIColor) {
self.title = title
self.color = color
}
}
然後,我做出分類空數組類型STRUCT
var categories = [Category]()
/* I appended some stuff manually to this array */
categories.append(Category(title: "Dram", color: Colors.cherry))
categories.append(Category(title: "Sport", color: Colors.lightGreen))
categories.append(Category(title: "Horror", color: Colors.purple))
categories.append(Category(title: "War", color: Colors.darkBlue))
/* Then I made a for loop */
for category in categories{
let button = UIButton()
button.createButton(title: category.title, titleColor: .white, fontType: "bold", fontSize: 17, background: category.color, cornerRadius: 4)
stackView.addArrangedSubview(button)
}
你應該使用收集視圖 –