标签云

微信群

扫码加入我们

WeChat QR Code

I'm trying to figure this out since last week without going any step further. Ok, so I need to apply some constraints programmatically in Swift to a UIView using this code:var new_view:UIView! = UIView(frame: CGRectMake(0, 0, 100, 100));new_view.backgroundColor = UIColor.redColor();view.addSubview(new_view);var constX:NSLayoutConstraint = NSLayoutConstraint(item: new_view, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0);self.view.addConstraint(constX);var constY:NSLayoutConstraint = NSLayoutConstraint(item: new_view, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0);self.view.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.view.addConstraint(constW);var constH:NSLayoutConstraint = NSLayoutConstraint(item: new_view, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: new_view, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0);self.view.addConstraint(constH);But Xcode returns this weird output:2014-10-03 09:48:12.657 Test[35088:2454916] 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) ("<NSLayoutConstraint:0x7fa4ea446830 UIView:0x7fa4ea429290.centerX == UIView:0x7fa4ea4470f0.centerX>","<NSAutoresizingMaskLayoutConstraint:0x7fa4ea4516c0 h=--& v=--& UIView:0x7fa4ea429290.midX == + 50>","<NSLayoutConstraint:0x7fa4ea452830 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7fa4ea4470f0(375)]>","<NSAutoresizingMaskLayoutConstraint:0x7fa4ea446db0 h=-&- v=-&- 'UIView-Encapsulated-Layout-Left' H:|-(0)-[UIView:0x7fa4ea4470f0] (Names: '|':UIWindow:0x7fa4ea444b20 )>")Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7fa4ea446830 UIView:0x7fa4ea429290.centerX == UIView:0x7fa4ea4470f0.centerX>Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in<UIKit/UIView.h> may also be helpful.2014-10-03 09:48:12.658 Test[35088:2454916] 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)("<NSLayoutConstraint:0x7fa4ea44d160 UIView:0x7fa4ea429290.centerY == UIView:0x7fa4ea4470f0.centerY>","<NSAutoresizingMaskLayoutConstraint:0x7fa4ea451b30 h=--& v=--& UIView:0x7fa4ea429290.midY == + 50>","<NSLayoutConstraint:0x7fa4ea44cf00 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7fa4ea4470f0(667)]>","<NSAutoresizingMaskLayoutConstraint:0x7fa4ea452700 h=-&- v=-&- 'UIView-Encapsulated-Layout-Top' V:|-(0)-[UIView:0x7fa4ea4470f0](Names: '|':UIWindow:0x7fa4ea444b20 )>")Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7fa4ea44d160 UIView:0x7fa4ea429290.centerY == UIView:0x7fa4ea4470f0.centerY>Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.Can you help me?Thanks a lot


Please paste error messages in as code blocks, not as quoted text.That means you need to put four spaces at the beginning of each line, not a >.I have fixed it for you this time.

2019年06月25日53分32秒

you are missing "translateAutoresizingMaskIntoConstraints = false"

2019年06月25日53分32秒

You may want to use this awesome library for dynamic and simple constraints. github.com/SnapKit/SnapKit

2019年06月25日53分32秒

Make sure you see On iOS, what are the differences between margins, edge insets, content insets, alignment rects, layout margins, anchors. It will improve your decision making between margins, anchors, layoutguides...

2019年06月25日53分32秒

Is there a way to animate the anchor style constraints?

2019年06月26日53分32秒

I didn't know about the option 5 / anchor style, I really like it because it fits best with the model of what I'm trying to do. But because I don't tend to update the constraints after they've been added I use a shorthand that activates them at the same time as creating them: e.g. newView.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor).active = true

2019年06月25日53分32秒

JoeHuang no, the short hand doesn't work if you want to change the constraint in the future because the shorthand is creating a new constraint. You'll need to use the anchor style Imanou uses in the answer if you want a reference to update a constraint in the future :)

2019年06月25日53分32秒

Amazed by the magical examples of Constraint Lord !! :)

2019年06月26日53分32秒

Don't forget the newView.translatesAutoresizingMaskIntoConstraints = false part! That took me a while to see.

2019年06月25日53分32秒

Very nice! One small note: only one of leading vs. centerX constraints should be specified.

2019年06月26日53分32秒

CyberDude, yes, you are correct. I meant for all of the examples above to be independent of each other, not adding all of the constraints to the same view. That is not as clear as I would have liked, though.

2019年06月25日53分32秒

You left out the necessary addConstraint() on view in order to make the layoutconstraints register.

2019年06月25日53分32秒

Cyrus, setting isActive = true also works.

2019年06月26日53分32秒

thank you for your kindness

2019年06月26日53分32秒

Based on this moment of WWDC video. It's generally not efficient to activate constraints one by one ie doing isActive = true is not always a good idea. It's better and more performant to group the related constraints into one and activate them all at once using NSLayoutConstraint.activate.

2019年06月25日53分32秒

if UIKit uses autoresizing internally could that explain misbehaviour of constraints that are anchored in the centre ? When I organise a collection of UIButtons as a subclass of UIview the anchor constraints appear to work but they do not work when I organise them as functions within a viewcontroller. If you can spare the time you might have a look at this post [stackoverflow.com/questions/40650951/…‌ ler-we-cant-actually-add-any-new-fences-due]

2019年06月26日53分32秒

No, that doesn't explain the misbehavior of your code.

2019年06月25日53分32秒

Don't forget to add, youViewName.translatesAutoresizingMaskIntoConstraints = false

2019年06月25日53分32秒

translatesAutoresizingMaskIntoConstraints = false :))

2019年06月26日53分32秒

Please explain the code and how it answers the question.

2019年06月25日53分32秒