Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change constraint second item

enter image description here

I have a view with this constraint. I want to animate it and move to bottom of NavigationBar. So i want to change Second item attribute to something like this:

UIView.animateWithDuration(15.5, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
    if (self.isViewLoaded() && self.view.window != nil) {
         //How to set bottom item ?
         self.storeSelectViewVerticalContainerConstraint.secondItem = self.navigationController?.view.bottom
         self.view.layoutIfNeeded()
    }
}, completion: {(Bool) in})

This code returns error:

Cannot assign to property: 'secondItem' is a get-only property

Or if this method is impossible, how to calculate, and move object from middle to top of view with navigation bar.

like image 416
Arti Avatar asked Feb 18 '16 11:02

Arti


People also ask

How do I change a constraint in Swift?

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. Method updateConstraints() is an instance method of UIView . It is helpful when you are setting the constraints programmatically.

How do I change the constraint multiplier in Swift?

Since multiplier is a read-only property and you can't change it, you need to replace the constraint with its modified clone.

How do I enable constraints in Swift?

addConstraint(constY); var constW:NSLayoutConstraint = NSLayoutConstraint(item: new_view, attribute: NSLayoutAttribute. Width, relatedBy: NSLayoutRelation. Equal, toItem: new_view, attribute: NSLayoutAttribute. Width, multiplier: 1, constant: 0); self.


2 Answers

You could add a second constraint with lower priority to start and then update both priorities when you need to:

UIView.animateWithDuration(15.5, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
    if (self.isViewLoaded() && self.view.window != nil) {
         self.storeSelectViewVerticalContainerConstraint.priority = UILayoutPriorityDefaultLow
         self.storeSelectViewTopContainerConstraint.priority = UILayoutPriorityDefaultHigh
         self.view.layoutIfNeeded()
    }
}, completion: {(Bool) in})

Remember to not move the priority from required to not required though, since that's not allowed. But moving from an optional value to another optional value is ok.

Alternatively you could just remove/add these constraints instead of changing their priority.

like image 106
fdiaz Avatar answered Nov 02 '22 20:11

fdiaz


Here Second Item is UIView(Main Root View), which is Y Axis Fixed. It can't be altered.

If you want animation of store selection view, you should change its constant from one value to another, and do

storeSelectViewVerticalContainerConstraint.constant = 0; // or any value you want.
UIView.animateWithDuration(5) {
    self.view.layoutIfNeeded()
}

Thanks. Happy Coding!

like image 2
Chatar Veer Suthar Avatar answered Nov 02 '22 20:11

Chatar Veer Suthar