jeudi 25 février 2016

Adding constraints programmatically to a M13CheckBox

I'm trying to incorporate a M13CheckBox into one of my views. I have successfully created one and placed it inside a view I call 'contentView.' The problem comes when I try to set constraints for the checkBox in the contentView.

When I add constraints, the checkBox moves but it becomes unresponsive at the same time.

Here is my code:

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)
}

So this all works but I can't actually click the checkBox when it apperars on screen.

So how could I move the checkBox while keeping it responsive?

I think it has something to do with me adding the checkBox.translatesAutoresizingMaskIntoConstraints = false. But when I don't have this the constraints don't work.

Any help would be appreciated. Thanks!




1 commentaire: