我必须在一个子视图中设置按钮最大按钮为3,最小值为零 我尝试设置水平按钮,但它总是相等的宽度,我尝试设置左对齐,依赖于按钮文本宽度,还有一个问题是,如果任何一个按钮丢失我的意思是说按钮数组计数是2和一个然后没有显示任何一个,请给我解决方案 这是我的代码
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]))
这样的名字数组
**ONE**
(
channelsnamelonggergo,
General
)
**two**
(
trndnl,
jive,
Banter
)
**Three**
(
channelsnamelonggergo,
Trending,
Local
)
**Four**
(
scoobydoobyparampaa,
channelsnamesarebigmerge,
enternewchannelsjive
)
请给我灵魂。
答案 0 :(得分:0)
如果我已经很好地理解了这个问题,你可以通过使用UIStackView并在其中创建三个UIButton来实现这一目的。
这是一个链接:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIStackView_Class_Reference/