以编程方式创建动态多个UIButton

时间:2017-08-10 11:30:23

标签: swift uibutton swift4

我正在以编程方式创建我的UI,这就是我创建按钮的方式

let button: UIButton = {

let button = UIButton()
button.setTitle("Try1",.normal)
return button

}

enter image description here

这就是我想要做的。例如,movie1有两个类别,Dram&恐怖我只是想展示其中的两个。 Movie2有4个类别,Dram& Horror& Sport&战争我只想展示他们中的4个。

现在我卡住了我不知道该怎么做。我知道你们会给减去代表,但我需要帮助。

完成!解决方案如下

首先我制作了一个结构

struct Category {
let title: String
let color: UIColor
init(title: String, color: UIColor) {
    self.title = title
    self.color = color
}
}

然后我创建了一个类型结构的空数组类型

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)
    }

enter image description here

3 个答案:

答案 0 :(得分:1)

使用Horizo​​ntal StackView。在StackView中,您可以根据需要添加尽可能多的排列子视图,它将自动调整到可用空间。只需设置StackView和rest的约束就可以开箱即用。

您可以使用以下方法将按钮添加到StackView:

stackView.addArangedSubview(button)

要创建按钮,您可以简单地使用循环:

for i in 1...3 { //change 3 to your value
    let button = UIButton()
    stackView.addArangedSubview(button)
}

答案 1 :(得分:1)

如果要在水平方向创建多个按钮,请在滚动视图中添加按钮

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)

}

答案 2 :(得分:-1)

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)
    }