0

I have a problem with a segmented control. In iOS 8.1 it works perfect, but in iOS 7.1 I get the error pasted below and the segmented control size change automatically every time I click on the others segmented controls that interact with that one. I deleted the segmented and created again but still happens. I'm adding a constraint to modify the segmented control height:

NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self.segmentedControlDistances
                                                              attribute:NSLayoutAttributeHeight
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:nil
                                                              attribute:NSLayoutAttributeNotAnAttribute
                                                             multiplier:1
                                                               constant:50];
[self.segmentedControlDistances addConstraint:constraint];

I get the following error:

"2015-04-13 12:48:58.751 KMetrix[507:607] 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:0x7bece3e0 V:[UISegmentedControl:0x7beca580(50)]>",
    "<NSIBPrototypingLayoutConstraint:0x7be86180 'IB auto generated at build time for view with fixed frame' H:[UISegmentedControl:0x7beca580(123)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7bece3e0 V:[UISegmentedControl:0x7beca580(50)]>

Break on objc_exception_throw 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 the IBOutlet created and also the IBAction:

    - (IBAction)segmentedControlDistances:(id)sender;
@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentedControlUnit, *segmentedControlSports, *segmentedControlDistances;

Thanks

Victor Rius
  • 4,566
  • 1
  • 20
  • 28

1 Answers1

1

The NSIBPrototypingLayoutConstraint is being added by IB, most likely due to IB thinking that you have not fully described the layout in the original layout. This fits with the fact that you are adding a constraint for this in your code, which clashes with the one added automatically.

My suggestion would be to add a constraint for the height in IB. Then CTRL drag this into the .h and create an IBOutlet. Give it a default value.

This now means you have a constraint for the height which will prevent IB adding one.

Now in your code, rather than adding a constraint simply set the constant of the constraints IBOutlet to the value you want. The result will be you can set the height in code without the constraint clash.


Alternatively, the following answer seems to cover all the issues and describes how to work around the problem of auto insertion of constraints.

Trouble with AutoLayout on UITableViewCell

Community
  • 1
  • 1
Rory McKinnel
  • 7,936
  • 2
  • 17
  • 28
  • How can I add a constraint for the height in IB and give to it a default value? I have the IB created as i edited on the question. – Victor Rius Apr 14 '15 at 16:19
  • Select the UISegmentedControl on storyboard, then click on the pin tool (symbol looks like a Star Wars Tie Fighter). Use the tool to add a constraint for the height. By default it will use what is drawn on the screen. On the view hierarchy, you then want to CTRL drag the height constraint (not the segmented control) into your .h file and create an IBOutlet for the constraint itself. This is what you change the value of in your code. – Rory McKinnel Apr 14 '15 at 16:38