user3899589 February 2016

swift Autolayout 3 button set horizontally width manage according to text

I have to set button in one child view max button is three and minimum is zero I Try to set Horizontally button but its take always equal width , I am Try to set left aligned , with depend on button text width , and one more problem is if any one button is missing i mean to say if button array count is two and one then not show any one , please give me solution Here is my code

 let button1  = UIButton()
                  let button2  = UIButton()
                 let button3  = UIButton()
            print(getChannelname)
            for var i = 0; i < getChannelname.count; i++ {

                if(i == 0)
                {
                    button1.translatesAutoresizingMaskIntoConstraints = false
                    button1.backgroundColor = UIColor.greenColor();
                    button1.tintColor = UIColor.blackColor()
                    button1.titleLabel?.textColor = UIColor.blackColor()
                    button1.setTitle("#\(getChannelname[i])" , forState: UIControlState.Normal)
                    button1.titleLabel!.font = UIFont(name: "Arial", size: 12)

                }else if (i == 1)
                {
                    button2.translatesAutoresizingMaskIntoConstraints = false
                    button2.backgroundColor = UIColor.yellowColor();
                    button2.tintColor = UIColor.blackColor()
                    button2.titleLabel?.textColor = UIColor.blackColor()
                    button2.setTitle("#\(getChannelname[i])" , forState: UIControlState.Normal)
                    button2.titleLabel!.font = UIFont(name: "Arial", size: 12)
                }else
                {

                    button3.translatesAutoresizingMaskIntoConstraints = false
                    button3.backgroundColor = UIColor.greenColor();
                    button3.tintColor = UIColor.blackColor()
                    button3.titleLabel?.textColor = UIColor.blackColor()
                    button3.setTitle("#\(getChannelname[i        

Answers


Nahouto February 2016

If I have well understood the question, you can manage to do this by using an UIStackView and creating the three UIButtons inside it.

Here is a link : https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIStackView_Class_Reference/

Post Status

Asked in February 2016
Viewed 3,360 times
Voted 13
Answered 1 times

Search




Leave an answer