Ios swift Autolayout 3按钮设置水平宽度根据文本管理
我必须在一个子视图中设置按钮,最大按钮是三个,最小按钮是零 我尝试设置水平按钮,但其宽度始终相等,我尝试设置左对齐,取决于按钮文本宽度,还有一个问题是,如果缺少任何一个按钮,我的意思是,如果按钮数组计数为2和1,则不显示任何一个,请给我解决方案 这是我的密码Ios swift Autolayout 3按钮设置水平宽度根据文本管理,ios,swift,autolayout,Ios,Swift,Autolayout,我必须在一个子视图中设置按钮,最大按钮是三个,最小按钮是零 我尝试设置水平按钮,但其宽度始终相等,我尝试设置左对齐,取决于按钮文本宽度,还有一个问题是,如果缺少任何一个按钮,我的意思是,如果按钮数组计数为2和1,则不显示任何一个,请给我解决方案 这是我的密码 let button1 = UIButton() let button2 = UIButton() let button3 = UIButton()
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])" , forState: UIControlState.Normal)
button3.titleLabel!.font = UIFont(name: "Arial", size: 12)
}
}
cell.viewChannel!.addSubview(button1)
cell.viewChannel!.addSubview(button2)
cell.viewChannel!.addSubview(button3)
cell.viewChannel!.layer.borderWidth = 2
let leftConstraint = NSLayoutConstraint(item: button1,
attribute: .Bottom,
relatedBy: .Equal,
toItem: cell.viewChannel!,
attribute: .Bottom,
multiplier: 1.0,
constant: 0.0);
cell.viewChannel!.addConstraint(leftConstraint);
cell.viewChannel!.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[b1]-0-[b2(==b1)]-0-[b3(==b1)]-0-|", options: NSLayoutFormatOptions.AlignAllBottom, metrics: nil, views: ["b1": button1, "b2": button2, "b3": button3]))
请给我解答。如果我已经很好地理解了这个问题,您可以使用UIStackView并在其中创建三个UIButton来做到这一点 以下是一个链接:
**ONE**
(
channelsnamelonggergo,
General
)
**two**
(
trndnl,
jive,
Banter
)
**Three**
(
channelsnamelonggergo,
Trending,
Local
)
**Four**
(
scoobydoobyparampaa,
channelsnamesarebigmerge,
enternewchannelsjive
)