以编程方式创建动态多个 UIButton
Creating dynamic multiple UIButton programmatically
我正在以编程方式创建我的 UI,这就是我创建按钮的方式
let button: UIButton = {
let button = UIButton()
button.setTitle("Try1",.normal)
return button
}
这就是我想做的。例如 movie1 有 2 个类别,Dram & Horror 我只想展示其中的 2 个。
Movie2 有 4 个类别,Dram&Horror&Sport& War 我只想展示其中的 4 个。
现在我卡住了,不知道该怎么办。我知道你们会给出负面评价,但我需要帮助。
完成!解决方案如下
首先我做了一个结构
struct Category {
let title: String
let color: UIColor
init(title: String, color: UIColor) {
self.title = title
self.color = color
}
}
然后我做了一个Category 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)
}
为此使用 Horizontal StackView。在 StackView 中,您可以根据需要添加尽可能多的排列子视图,它会自动调整到可用 space。只需为 StackView 设置约束,其余的就可以开箱即用。
您可以使用以下方法将按钮添加到 StackView:
stackView.addArangedSubview(button)
要创建按钮,您可以简单地使用 for 循环:
for i in 1...3 { //change 3 to your value
let button = UIButton()
stackView.addArangedSubview(button)
}
Swift 3.0 ...
let stackview = UIStackView(frame: CGRect(x: 0, y: 0, width: 512, height: 356)) // set your postion
stackview.axis = . Horizontal
stackview.spacing = 10.0
stackview.alignment = .Fill
stackview.distribution = .EqualSpacing
for i in 1...3 {
let button = UIButton()
stackview.addArangedSubview(button)
}
如果您想在水平方向上创建多个按钮,请在滚动视图中添加按钮
Swift 4.0
func ButtonCreation() {
yourScrollviewname.isScrollEnabled = true
yourScrollviewname.isUserInteractionEnabled = true
let numberOfButtons = 16
let numberofRows = 1
var count = 0
var px = 0
var py = 0
for _ in 1...numberofRows {
px = 0
if count < numberOfButtons {
for j in 1...numberOfButtons{
count += 1
let Button = UIButton()
Button.tag = count
Button.frame = CGRect(x: px, y: 0, width: 80, height: 27)
Button.layer.borderWidth = 1.0
Button.layer.masksToBounds = true
Button.setTitleColor(UIColor.black, for: .normal)
print(colorArr[j-1])
Button.setTitle(colorText[j-1], for: .normal)
Button.addTarget(self, action: #selector(scrollButtonAction), for: .touchUpInside)
yourScrollviewname.addSubview(Button)
px = px + Int(yourScrollviewname.frame.width)/2 - 50
}
}else{
for j in numberOfButtons/2...numberOfButtons {
count += 1
let Button = UIButton()
Button.tag = count
Button.frame = CGRect(x: px+10, y: py+10, width: 80, height: 427)
Button.backgroundColor = UIColor.black
Button.setTitle("Hello \(j) ", for: .normal)
Button.addTarget(self, action: #selector(scrollButtonAction), for: .touchUpInside)
yourScrollviewname.addSubview(Button)
px = px + Int(yourScrollviewname.frame.width)/2 - 30
}
}
py = Int(yourScrollviewname.frame.height)-70
}
yourScrollviewname.contentSize = CGSize(width: px, height: py)
}
我正在以编程方式创建我的 UI,这就是我创建按钮的方式
let button: UIButton = {
let button = UIButton()
button.setTitle("Try1",.normal)
return button
}
这就是我想做的。例如 movie1 有 2 个类别,Dram & Horror 我只想展示其中的 2 个。 Movie2 有 4 个类别,Dram&Horror&Sport& War 我只想展示其中的 4 个。
现在我卡住了,不知道该怎么办。我知道你们会给出负面评价,但我需要帮助。
完成!解决方案如下
首先我做了一个结构
struct Category {
let title: String
let color: UIColor
init(title: String, color: UIColor) {
self.title = title
self.color = color
}
}
然后我做了一个Category 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)
}
为此使用 Horizontal StackView。在 StackView 中,您可以根据需要添加尽可能多的排列子视图,它会自动调整到可用 space。只需为 StackView 设置约束,其余的就可以开箱即用。
您可以使用以下方法将按钮添加到 StackView:
stackView.addArangedSubview(button)
要创建按钮,您可以简单地使用 for 循环:
for i in 1...3 { //change 3 to your value
let button = UIButton()
stackView.addArangedSubview(button)
}
Swift 3.0 ...
let stackview = UIStackView(frame: CGRect(x: 0, y: 0, width: 512, height: 356)) // set your postion
stackview.axis = . Horizontal
stackview.spacing = 10.0
stackview.alignment = .Fill
stackview.distribution = .EqualSpacing
for i in 1...3 {
let button = UIButton()
stackview.addArangedSubview(button)
}
如果您想在水平方向上创建多个按钮,请在滚动视图中添加按钮
Swift 4.0
func ButtonCreation() {
yourScrollviewname.isScrollEnabled = true
yourScrollviewname.isUserInteractionEnabled = true
let numberOfButtons = 16
let numberofRows = 1
var count = 0
var px = 0
var py = 0
for _ in 1...numberofRows {
px = 0
if count < numberOfButtons {
for j in 1...numberOfButtons{
count += 1
let Button = UIButton()
Button.tag = count
Button.frame = CGRect(x: px, y: 0, width: 80, height: 27)
Button.layer.borderWidth = 1.0
Button.layer.masksToBounds = true
Button.setTitleColor(UIColor.black, for: .normal)
print(colorArr[j-1])
Button.setTitle(colorText[j-1], for: .normal)
Button.addTarget(self, action: #selector(scrollButtonAction), for: .touchUpInside)
yourScrollviewname.addSubview(Button)
px = px + Int(yourScrollviewname.frame.width)/2 - 50
}
}else{
for j in numberOfButtons/2...numberOfButtons {
count += 1
let Button = UIButton()
Button.tag = count
Button.frame = CGRect(x: px+10, y: py+10, width: 80, height: 427)
Button.backgroundColor = UIColor.black
Button.setTitle("Hello \(j) ", for: .normal)
Button.addTarget(self, action: #selector(scrollButtonAction), for: .touchUpInside)
yourScrollviewname.addSubview(Button)
px = px + Int(yourScrollviewname.frame.width)/2 - 30
}
}
py = Int(yourScrollviewname.frame.height)-70
}
yourScrollviewname.contentSize = CGSize(width: px, height: py)
}