Swift: programatically copy auto layout constraints from one view to another

Viewed 5269

I have a UIButton that I set up with auto layout constraints in a storyboard. i also have a UIView that I initiate in the UIViewController's viewDidLoad method. I make this view have (almost) all the same properties as the UIButton but when it I run it in the simulator it doesn't "stick" to the button. here's what I have:

class ViewController: UIViewController {

    @IBOutlet weak var someButton: UIButton!

    func viewDidLoad() {
        super.viewDidLoad()

        let someView = UIView()
        someView.backgroundColor = UIColor.greenColor()
        someView.frame = someButton.bounds
        someView.frame.origin = someButton.frame.origin
        someView.autoresizingMask = someButton.autoresizingMask
        someView.autoresizesSubviews = true
        someView.layer.cornerRadius = someButton.layer.cornerRadius
        someView.clipsToBounds = true
        someView.userInteractionEnabled = false
        view.insertSubview(someView, belowSubview: someButton)
    }

}

I guess I’m missing the someView.auto layout constraint?

EDIT: i thought accessing the UIButton's constraints would work but they appear to be an empty array. are story board constraints are hidden?

someView.addConstraints(someButton.constraints)

thanks.

1 Answers
Related