0

I am creating my UI programmatically, and this is how I create a button

let button: UIButton = {

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

}

enter image description here

And this is what I want to do. For example movie1 has got 2 categories, Dram & Horror I just want to show 2 of them. Movie2 has got 4 categories, Dram&Horror&Sport& War i just want to show 4 of them.

And now Im stuck I have NO idea what to do. I know you guys gonna give minus rep but I need help.

Done! Solution is below

First I have made a struct

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

Then I made an empty array type of 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)
    }

enter image description here

1
  • 1
    you should use collection view Commented Aug 10, 2017 at 11:40

3 Answers 3

2

Use Horizontal StackView for that. In StackView you can add as much arranged subviews as you want and it will be automatically adjusting to the available space. Just setup constraints for StackView and rest will just work out of the box.

You can add your button to StackView using:

stackView.addArangedSubview(button)

To create button you can simple use for loop:

for i in 1...3 { //change 3 to your value
    let button = UIButton()
    stackView.addArangedSubview(button)
}
Sign up to request clarification or add additional context in comments.

9 Comments

Well im actually looking for creating buttons. What I want is creating buttons for the data. If the response data is like = ["Horror","Sport"] I'd like to create 2 button automatically.
You can create the buttons according to data using a stackview also.
I've edited my answer with a for loop that will create a buttons for you. It's really easy
Uhm, its hard to figure it out. Im going to explain it: I want to have one button, for example with 100x200px and background color is black and title is Button. Then according to the data I want to multiple it for example change background color to blue and title Data1, background color red and title Data2
So create an array of your titles and colors and while creating buttons get those data from arrays. This is not the best solution but I think the easiest one for you
|
1

If you want to create multiple buttons in horizontal then add button in scrollview

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)

}

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.