iOS以编程方式添加约束导致错误

标签 ios objective-c constraints

我在我的应用程序中有这个 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 the UIView property translatesAutoresizingMaskIntoConstraints)

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

相关文章:

ios - iOS 中的等宽字体是什么?

ios - BFTASK中的方法即使完成任务也会被延迟

ios - 在两个 View 之间添加尾随约束

ios - 无法在 UIStackView 中设置编程布局约束

grails - 如何在多个列上使用唯一约束而不是创建复合键

ios - CALayer 上的图像在缩小然后再次放大后变得模糊

iphone - CAGradientLayer 的问题

iphone - 应用提交,图标大小错误由于即将发布的IOS7

ios - objective-c :方法调用查询

iphone - 如何在 iOS 6 中获取 Facebook 好友?