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

Posted

技术标签:

【中文标题】无法同时满足分段控制中的约束【英文标题】:Unable to simultaneously satisfy constraints in segmented control 【发布时间】:2015-04-13 11:08:47 【问题描述】:

我对分段控件有疑问。在 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;

谢谢

【问题讨论】:

【参考方案1】:

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

我的建议是为 IB 中的高度添加一个约束。然后 CTRL 将其拖入 .h 并创建一个 IBOutlet。给它一个默认值。

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

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


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

Trouble with AutoLayout on UITableViewCell

【讨论】:

如何在 IB 中为高度添加约束并为其赋予默认值?我在编辑问题时创建了 IB。 在情节提要上选择 UISegmentedControl,然后单击 pin 工具(符号看起来像 Star Wars Tie Fighter)。使用该工具为高度添加约束。默认情况下,它将使用在屏幕上绘制的内容。然后,在视图层次结构中,您希望 CTRL 将高度约束(不是分段控件)拖到您的 .h 文件中,并为约束本身创建一个 IBOutlet。这就是您在代码中更改的值。

以上是关于无法同时满足分段控制中的约束的主要内容,如果未能解决你的问题,请参考以下文章

以编程方式向 Swift 中的视图添加约束时出错

无法同时满足约束。可能以下列表中的至少一个约束是您不想要的

以编程方式应用自动布局约束时无法同时满足约束

无法使用 uiimageview 同时满足 uitableviewcell 中的约束

SnapKit 自动布局损坏:“无法同时满足约束”

iOS:无法同时满足约束