Update Storyboard Constraint Using Code

How to change constraints programmatically that is added from storyboard?

You need to create an IBOutlet of your constraint.

Sample Image

Then you set the constant value of your constraint in code:

labelWidthConstraint.constant = newValue

If you want it animated you can do something like this:

Swift

labelWidthConstraint.constant = newValue
UIView.animate(withDuration: 0.3, animations: {
self.view.layoutIfNeeded()
})

Objective-C

self.labelWidthConstraint.constant = newValue;
[UIView animateWithDuration:0.3 animations:^{
[self.view layoutIfNeeded];
}];

Update storyboard constraint using code

You should to call layoutIfNeeded within the animation block. Apple actually recommends you call it once before the animation block to ensure that all pending layout operations have been completed. I just checked it with the resizing of button - everything works fine.

@IBOutlet weak var myBtn: UIButton!
@IBOutlet weak var btnHeight: NSLayoutConstraint!
@IBOutlet weak var btnWidth: NSLayoutConstraint!

@IBAction func resizeBtn(sender: AnyObject) {
self.myBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0)

self.view.layoutIfNeeded()
self.btnHeight.constant += 50
self.btnWidth.constant += 50

UIView.animateWithDuration(0.7, animations: {
self.view.layoutIfNeeded()
})
}

P.S. make sure that other constraints don't block your changes.

How to update the constant height constraint of a UIView programmatically?

Select the height constraint from the Interface builder and take an outlet of it. So, when you want to change the height of the view you can use the below code.

yourHeightConstraintOutlet.constant = someValue
yourView.layoutIfNeeded()

Method updateConstraints() is an instance method of UIView. It is helpful when you are setting the constraints programmatically. It updates constraints for the view. For more detail click here.

Updating Main.storyboard constraints programatically (Swift)

A constraint relating the bottom anchor of a text view and its super view's bottom anchor will be added to the text view's super view, so you cannot find it in textView.constraints.

A much simpler way to do this is to add an IBOutlet from the storyboard:

@IBOutlet var bottomConstraint: NSLayoutConstraint!

Right click on your view controller in the storyboard, then connect bottomConstraint to the constraint shown in the outline view:

Sample Image

Then you can remove your entire for loop and replace it with just:

bottomConstraint.constant = keyboardSize.height

How to update constraints programmatically if I would like to change every time

Since you are using SnapKit, you need to use its .updateConstraints syntax.

This example will toggle .imageConstraint between 50.0 and -50.0 on any touch, and animate the image view:

class SnapVC: UIViewController {

let imageView = UIImageView()

private var imageConstraint = 50.0

override func viewDidLoad() {
super.viewDidLoad()

if let img = UIImage(named: "myImage") {
imageView.image = img
}

view.addSubview(imageView)

imageView.snp.makeConstraints {
$0.width.height.equalTo(48)
$0.centerX.equalToSuperview()
$0.bottom.equalTo(-imageConstraint)
}

}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
imageConstraint *= -1
imageView.snp.updateConstraints {
$0.bottom.equalTo(-imageConstraint)
}
UIView.animate(withDuration: 0.5, animations: {
self.view.layoutIfNeeded()
})
}

}

Is there a way to add constraints into the xcode storyboard with code?

Yes. The respective classes to use are 'NSLayoutConstraint' and 'NSLayoutAnchor'. It is described in the programming guide "Auto Layout Guide".

BTW:
I just found another question that actually shows some code how to use it:
How to set UITextField width constraint to have room for a certain string

Update layout constraints programmatically without IBOutlets - Swift

Store the constraint in a variable and change the constant and call layoutIfNeeded when you need it animated.

// Declare this along with the other variables in your class
var constraintVariable: NSLayoutConstraint!

.
.
.

// Where you set your constraints. Store the constraint to be animated in the variable and make it active
// Your other constraints
constraintVariable = sideMenu.leftAnchor.constraint(equalTo: view.leftAnchor, constant: someNegativeValue);
constraintVariable.isActive = true

.
.
.

@objc func slideMenu() {
UIView.animate(withDuration: suitableDuration) {
constraintVariable.constant = requiredValue
view.setNeedsLayout()
view.layoutIfNeeded()
}
}


Related Topics



Leave a reply



Submit