gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:07:52 25 4
gpt4 key购买 nike

我对分段控件有疑问。在 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 的常量 设置为您想要的值即可。结果将是您可以在代码中设置高度而不会发生约束冲突。


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

Trouble with AutoLayout on UITableViewCell

关于ios - 无法同时满足分段控制中的约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29603862/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com