汽车中心以编程方式创建UIButtons

时间:2015-10-12 08:54:27

标签: ios swift uibutton swift2

我怎样才能确保我的观点(在这种情况下是UIButton)总是在中间居中,无论我有多少? (见图例)

enter image description here

我在循环中以编程方式创建它们。但这种方式只有在有4个按钮时才有效。

var i = 220

for button in optionButtons {
    button.frame = CGRectMake(CGFloat(i), 280, 150, 150)
    button.translatesAutoresizingMaskIntoConstraints = false
    button.addTarget(self, action: "pressedOption:", forControlEvents: .TouchUpInside)
    dispatch_async(dispatch_get_main_queue(), {
        self.view.addSubview(button)
    })
    CGFloat(i += 150)
}

但这只有在有4个时才有效。如何使用少于4个来完成这项工作?

2 个答案:

答案 0 :(得分:0)

这是我的解决方案:

构建一个包含所有按钮的超级视图。 enter image description here

接下来,您只需要以superView为中心。

更新:如何自动获取superView的大小?

首先,设置superView的位置很容易。

然后,如果您的按钮的大小和它们之间的空间是固定的。 superView的宽度和高度可以由childViews决定。

enter image description here

随意问我。谢谢!

答案 1 :(得分:0)

您可以以编程方式创建自动布局格式字符串并向视图添加约束。

let button1 = UIButton()
button1.translatesAutoresizingMaskIntoConstraints = false
button1.setTitle("button 1", forState: UIControlState.Normal)
button1.backgroundColor = UIColor.redColor()

let button2 = UIButton()
button2.translatesAutoresizingMaskIntoConstraints = false
button2.setTitle("button 2", forState: UIControlState.Normal)
button2.backgroundColor = UIColor.redColor()

let button3 = UIButton()
button3.translatesAutoresizingMaskIntoConstraints = false
button3.setTitle("button 3", forState: UIControlState.Normal)
button3.backgroundColor = UIColor.redColor()

let buttons = [button1, button2, button3]
let spaceBeetweenButtons = NSNumber(int: 10)
let buttonWidth = NSNumber(int: 80)
let buttonHeight = NSNumber(int: 35)

let containerWidth = NSNumber(integer: (buttons.count * buttonWidth.integerValue) + (buttons.count - 1) * spaceBeetweenButtons.integerValue)
let containerView = UIView()
containerView.translatesAutoresizingMaskIntoConstraints = false

var views = [String : AnyObject]()
let metrics = ["buttonWidth" : buttonWidth, "buttonHeight" : buttonHeight, "spaceBeetweenButtons" : spaceBeetweenButtons, "containerWidth" : containerWidth]
var format = "H:|-0-"

for var i = 0; i < buttons.count; i++
{
    views["button\(i)"] = buttons[i]
    format += "[button\(i)(==buttonWidth)]"
    if i != buttons.count - 1
    {
        format += "-spaceBeetweenButtons-"
    }
    else
    {
        format += "-0-|"
    }
    containerView.addSubview(buttons[i])
    containerView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[button\(i)(==buttonHeight)]", options: NSLayoutFormatOptions.DirectionLeftToRight, metrics: metrics, views: views))
}

containerView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(format, options: NSLayoutFormatOptions.DirectionLeftToRight, metrics: metrics, views: views))

self.view.addSubview(containerView)
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[containerView(==buttonHeight)]", options: NSLayoutFormatOptions.DirectionLeftToRight, metrics: metrics, views: ["containerView" : containerView]))
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[containerView(==containerWidth)]", options: NSLayoutFormatOptions.DirectionLeftToRight, metrics: metrics, views: ["containerView" : containerView]))
self.view.addConstraint(NSLayoutConstraint(item: containerView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0.0))
self.view.addConstraint(NSLayoutConstraint(item: containerView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0.0))

结果: