Search code examples
iosswiftuistoryboardsnapkit

SnapKit. Replace storyboard constrains


If I create UIButton instance in code and apply constrains using Snapkit framework - it is working as expected. But when I try apply the same code to @IBOutlet button (defined in storyboard), nothing happen - I see button on the same position / size as it was on storyboard...

Sample code:

    let button = UIButton()
    self.view.addSubview(button)

or

@IBOutlet weak var button: UIButton!

and constrains are

    button.snp_makeConstraints { make in
        make.width.equalTo(150)
        make.height.equalTo(50)
        make.centerX.equalTo(self.view.snp_centerX)
        make.centerY.equalTo(self.view.snp_centerY).offset(-40)
    }

I have tried to call button.snp_removeConstraints() before snp_makeConstraints() method without success.


Solution

  • SnapKit removes only own constraints.For storyboard constraints you need remove view from superview or use this snippet to clear all default constraints

    view.removeConstraints(view.constraints)