无法同时满足分段控制中的约束

Unable to simultaneously satisfy constraints in segmented control

分段控件有问题。在 iOS 8.1 中,它运行完美,但在 iOS 7.1 中,我收到下面粘贴的错误,每次我单击与该控件交互的其他分段控件时,分段控件大小都会自动更改。 我删除了分段并再次创建但仍然发生。 我正在添加一个约束来修改分段控制高度:

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

我收到以下错误:

"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"

我创建了 IBOutlet 和 IBAction:

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

谢谢

IB 正在添加 NSIBPrototypingLayoutConstraint,很可能是因为 IB 认为您没有完整描述原始布局中的布局。这符合您在代码中为此添加约束的事实,这与自动添加的约束冲突。

我的建议是在 IB 中添加一个高度限制。然后按 CTRL 将其拖到 .h 中并创建一个 IBOutlet。给它一个默认值。

现在这意味着您对高度有限制,这将阻止 IB 添加一个。

现在在您的代码中,无需添加约束,只需将约束 IBOutlet 的 constant 设置为您想要的值即可。结果将是您可以在代码中设置高度而不会发生约束冲突。


或者,以下答案似乎涵盖了所有问题,并描述了如何解决自动插入约束的问题。

Trouble with AutoLayout on UITableViewCell