1

As i posted in this GitHub issue, I am having trouble with some hidden constraints that throw an error whenever I try to add my own constraints

2016-04-18 19:22:04.270 Metric Time[1519:447809] 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)
(
"<NSAutoresizingMaskLayoutConstraint:0x15e670e70 h=--& v=--& Metric_Time.View:0x15e692e70.midX == + 115>",
"<NSLayoutConstraint:0x15e5048c0 Metric_Time.View:0x15e692e70.centerX == UIView:0x15e6953e0.centerX>",
"<NSAutoresizingMaskLayoutConstraint:0x15e55fd30 h=-&- v=-&- 'UIView-Encapsulated-Layout-Left' H:|-(0)-[UIView:0x15e6953e0]   (Names: '|':UIWindow:0x15e688ed0 )>",
"<NSLayoutConstraint:0x15e54e750 'UIView-Encapsulated-Layout-Width' H:[UIView:0x15e6953e0(320)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x15e5048c0 Metric_Time.View:0x15e692e70.centerX == UIView:0x15e6953e0.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.`

I have tried looking for the places that these constraints could have been created, but searching my ViewController.swift file for keywords such as constraint, layout .etc turned up only the code that I wrote.

My code is all on GitHub if anyone wants to mess around with it.

MoralCode
  • 1,954
  • 1
  • 20
  • 42

1 Answers1

3

If you are going to add constraints programmatically, you need to turn off translateAutoresizingMaskIntoConstraints. You also don't have enough constraints to satisfy the layout engine. Remember that UIView's don't have an intrinsic size and if you are using auto layout with a view you need to set everything with constraints and not rely on a mixture of constraints and frames. one possible solution is to add some code like:

        self.view.addSubview(clockView)
        clockView.translatesAutoresizingMaskIntoConstraints = false
        clockView.centerYAnchor.constraintEqualToAnchor(self.view.centerYAnchor, constant: 0.0).active = true
        clockView.centerXAnchor.constraintEqualToAnchor(self.view.centerXAnchor, constant: 0.0).active = true
        clockView.widthAnchor.constraintEqualToConstant(230.0).active = true
        clockView.heightAnchor.constraintEqualToConstant(230.0).active = true

However this is not exactly what you want as when it is in landscape your clock overlays your label. But you could fix that by playing with clockView.centerYAnchor.constraintEqualToAnchor(self.view.centerYAnchor, constant: 0.0).active = true

beyowulf
  • 15,101
  • 2
  • 34
  • 40
  • what if i was to use this on a `UIDatePicker()`? – MoralCode Apr 25 '16 at 03:57
  • You can do the same thing, but replace clockView with datePicker. You also don't need to add width and height constraints, as a UIDatePicker has an intrinsic size. – beyowulf Apr 25 '16 at 12:55
  • ok, also @beyowulf, my UIDatePicker is looking a little weird when i inspect the view hierarchy... http://imgur.com/y0sq2RE https://gist.github.com/DeveloperACE/f551f8783e1fc01d600da3377c5308cd what am i doing wrong? – MoralCode Apr 25 '16 at 13:03
  • @ACE That is just how UIPickerViews are implemented. You don't need to worry, in general Apple frameworks are highly optimized and robust. I opened a pull request for your repo https://github.com/DeveloperACE/MetricTime/pull/7 You should consider these changes. – beyowulf Apr 26 '16 at 02:52
  • is there an iOS 8 compatible way to do this? – MoralCode Apr 28 '16 at 04:26
  • Yes, use the older way of setting constraints. – beyowulf Apr 28 '16 at 13:58
  • You can see here: https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/ProgrammaticallyCreatingConstraints.html, or use a third party library like PureLayout. I would say less than 20% of devices are running iOS 8. Apple is about to unveil iOS 10, so unless you're planning on releasing to the app store like now, it's probably not worth it to try to add support, but in your project, it would be easier to add your view to your storyboard and add constraints there. You not doing anything that prohibits that. – beyowulf Apr 28 '16 at 22:09