以编程方式将约束添加到M13CheckBox

时间:2016-02-25 13:56:17

标签: ios swift checkbox uistoryboard

我试图将M13CheckBox合并到我的一个视图中。我已经成功创建了一个并将其放在我称之为内容视图的视图中。'当我尝试在contentView中为checkBox设置约束时出现问题。

当我添加约束时,checkBox会移动,但它会同时无响应。

这是我的代码:

override func viewDidLoad() {
    super.viewDidLoad()

    //create the checkBox

    let size = CGRectMake(0, 0, 18, 18)
    let checkBox = M13Checkbox.init(frame: size)
    checkBox.strokeColor = UIColor.init(red: 189, green: 170, blue: 170)
    checkBox.checkColor = UIColor.init(red: 74, green: 74, blue: 74)
    checkBox.tintColor = UIColor.init(red: 189, green: 170, blue: 170)
    checkBox.translatesAutoresizingMaskIntoConstraints = false

    //add to contentView which is already setup in the storyboard

    contentView.addSubview(checkBox)

    //add the constraints

    let views = ["view": contentView, "checkBox": checkBox]
    let horizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|-20-[checkBox]", options: NSLayoutFormatOptions.AlignAllCenterY, metrics: nil, views: views)
    contentView.addConstraints(horizontalConstraints)
    let verticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("V:|-10-[checkBox]", options: NSLayoutFormatOptions.AlignAllCenterX, metrics: nil, views: views)
    contentView.addConstraints(verticalConstraints)
}

所以这一切都有效,但我无法在屏幕上显示屏幕时点击checkBox。

那么我怎么能在保持响应的同时移动checkBox呢?

我认为这与我添加 checkBox.translatesAutoresizingMaskIntoConstraints = false。有关。但是当我没有这个时,约束就不起作用了。

任何帮助将不胜感激。谢谢!

0 个答案:

没有答案
相关问题