1

I'm having a problem with updating Constraints.

I add contraint by using this code.

var oldHeight = 92 + ((catAmount-10)*100)

self.mainViewport.addConstraint(NSLayoutConstraint(item: self.mainViewport, attribute: .Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0, constant: CGFloat(oldHeight)))

This code works 100%, but when user scroll down i need to add more info to ViewController so I need to update constraints to. but I get ant error.

Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "", "" )

I've tried to remove constraint by this code:

self.mainViewport.removeConstraint(NSLayoutConstraint(item: self.mainViewport, attribute: .Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0, constant: CGFloat(oldHeight)))

but it don't remove the old one so I can't add new or update constraint.

What I'm doing wrong? How I can update constraint continuously when i need?

P.S. I'm using ScrollViewController and a ViewController with info in it.

Code update.

class CategoryViewController: UIViewController, UIScrollViewDelegate {
var CategoriesConstraint : NSLayoutConstraint!
/**  **/
var aukstis = 92 + (catAmount*100)


        self.mainViewport.setTranslatesAutoresizingMaskIntoConstraints(false)

        self.CategoriesConstraint = NSLayoutConstraint(item: self.mainViewport, attribute: .Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0, constant: CGFloat(aukstis))

        if(catAmount == 10){
            self.mainViewport.addConstraint(self.CategoriesConstraint)
            self.showApp(1,secondJson: 0)
        } else {
            self.CategoriesConstraint.constant = CGFloat(aukstis)
            self.mainViewport.updateConstraints()
            self.mainViewport.setNeedsLayout()
            self.mainViewport.layoutIfNeeded()
        }
1
  • Finally founded my mistake Commented Jan 15, 2015 at 15:42

1 Answer 1

1

You need to retain a reference to the original constraint (returned when you created it) so that you can update it (preferably, or remove it if you need to).

Note that the removal doesn't work in your current code because you are creating a new constraint and trying to remove it, but it isn't actually attached to the view yet.

self.xxxConstraint = NSLayoutConstraint(item: self.mainViewport, attribute: .Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0, constant: CGFloat(oldHeight))

self.mainViewport.addConstraint(self.xxxConstraint)
Sign up to request clarification or add additional context in comments.

8 Comments

I've tried to do this: self.mainViewport.removeConstraint(NSLayoutConstraint(item: self.mainViewport, attribute: .Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0, constant: CGFloat(OLD-Height))) self.mainViewport.addConstraint(NSLayoutConstraint(item: self.mainViewport, attribute: .Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0, constant: CGFloat(NEW-Height))) But still nothing...
you need to add a property to hold the constraint, create the constraint (and store it) and then add it. then you can use the property to modify / remove it in future
I'm new on Swift ant iOS. can you show my some sample?
I've updated the answer (I don't really write swift either) - if you aren't clear on what properties are you should read the docs some more: developer.apple.com/library/ios/documentation/Swift/Conceptual/…
No, it is different. Now you have self.xxxConstraint that you can use again later to remove or to change self.xxxConstraint.constant
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.