简体   繁体   English

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

[英]iOS programmatically adding constraints resulting in error

I have this TabBarController in my app to which I've added an UIView on the very top of the TabBar. 我在我的应用程序中有这个TabBarController,我在TabBar的顶部添加了一个UIView。 I did this because this view shows persistent data that I wanted to display in every view. 我这样做是因为这个视图显示了我希望在每个视图中显示的持久数据。 I initialized this using initWithFrame: and it is just where I wanted it to be. 我使用initWithFrame:初始化它initWithFrame:它就是我想要它的地方。

Today I noticed that when the status bar increases in height (during a call or personal hotspot), that view moves towards the bottom. 今天我注意到当状态栏的高度增加时(在通话或个人热点期间),该视图会向下移动。 I tried to fix that by adding a constraint: 我尝试通过添加约束来解决这个问题:

[[self view] addConstraints:[NSLayoutConstraint 
                     constraintsWithVisualFormat:@"V:[_myView]-(44)-|"
                                         options:NSLayoutFormatDirectionLeadingToTrailing
                                         metrics:nil
                                           views:NSDictionaryOfVariableBindings(_myView)]];

This way, I'd expect that myView would always be spaced by 44 pts from the bottom of the screen, however it does not happen. 这样,我希望myView总是从屏幕底部间隔44点,但不会发生。

The debugger shows: 调试器显示:

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; 试试这个:(1)看看每个约束并试着找出你不期望的东西; (2) find the code that added the unwanted constraint or constraints and fix it. (2)找到添加了不需要的约束或约束的代码并修复它。 (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints ) (注意:如果您看到您不理解的NSAutoresizingMaskLayoutConstraints ,请参阅UIView属性的文档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 )> 将尝试通过破坏约束来恢复< NSLayoutConstraint:0x165c0280 V:[UIView:0x165bda30]-(44)-| (Names: '|':UIView:0x166dd460 )> < NSLayoutConstraint:0x165c0280 V:[UIView:0x165bda30]-(44)-| (Names: '|':UIView:0x166dd460 )>

Break on objc_exception_throw to catch this in the debugger. 打破objc_exception_throw以在调试器中捕获它。 The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful. 在列出的UIView上的UIConstraintBasedLayoutDebugging类别中的方法也可能有所帮助。

This error only shows when the status bar is extended. 此错误仅在状态栏扩展时显示。 There are no other constraints added programmatically, and the Interface Builder's VC for this class is empty. 没有以编程方式添加其他约束,并且此类的Interface Builder的VC为空。 Everything is added programmatically. 一切都是以编程方式添加的。

This is driving me nuts! 这让我疯了!

You need to call setTranslatesAutoresizingMaskIntoConstraints: on the view with a parameter of NO . 您需要在视图上调用setTranslatesAutoresizingMaskIntoConstraints:参数为NO This will prevent the automatic addition of the NSAutoresizingMaskLayoutConstraint s that you see in the log (so only your explicit constraints will apply). 这将阻止您在日志中看到的NSAutoresizingMaskLayoutConstraint的自动添加(因此只应用您的显式约束)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM