我在我的应用程序中有这个 TabBarController,我在 TabBar 的最顶部添加了一个 UIView。我这样做是因为这个 View 显示了我想在每个 View 中显示的持久数据。
我使用 initWithFrame:
对其进行了初始化这正是我想要的地方。
今天我注意到当状态栏的高度增加时(在通话或个人热点期间),该 View 向底部移动。我试图通过添加一个约束来解决这个问题:
[[self view] addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"V:[_myView]-(44)-|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:NSDictionaryOfVariableBindings(_myView)]];
这样,我希望 myView 始终与屏幕底部相距 44 pts,但它不会发生。
调试器显示:
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 theUIView
propertytranslatesAutoresizingMaskIntoConstraints
)( "< NSLayoutConstraint:0x165c0280 V:[UIView:0x165bda30]-(44)-| (Names: '|':UIView:0x166dd460 )>", "< NSAutoresizingMaskLayoutConstraint:0x165a7ce0 h=--& v=--& UIView:0x165bda30.midY == + 507>", "< NSAutoresizingMaskLayoutConstraint:0x165c50c0 h=--& v=--& V:[UIView:0x165bda30(34)]>", "< NSAutoresizingMaskLayoutConstraint:0x165c3a90 h=-&- v=-&- UIView:0x166dd460.height == UIWindow:0x1668f870.height - 20>", "< NSAutoresizingMaskLayoutConstraint:0x165a8070 h=--- v=--- V:[UIWindow:0x1668f870(568)]>" )
Will attempt to recover by breaking constraint
< NSLayoutConstraint:0x165c0280 V:[UIView:0x165bda30]-(44)-| (Names: '|':UIView:0x166dd460 )>
Break on objc_exception_throw to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful.
此错误仅在状态栏扩展时显示。没有以编程方式添加其他约束,并且此类的 Interface Builder 的 VC 是空的。一切都是以编程方式添加的。
这让我发疯!
最佳答案
您需要调用setTranslatesAutoresizingMaskIntoConstraints:
在参数为 NO
的 View 上.这将阻止自动添加 NSAutoresizingMaskLayoutConstraint
您在日志中看到的 s(因此只有您的显式约束将适用)。
关于iOS以编程方式添加约束导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20243047/