xcode 在尺寸等级中获得约束

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24482408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:08:56  来源:igfitidea点击:

Getting constraints to work in size classes

iosxcode

提问by Atharv Vaish

So, I'm playing around with size classes in Xcode 6 beta. I set up some constraints on an image such that it is in a different position based on size classes corresponding to iPhone portrait and landscape.

所以,我在 Xcode 6 beta 中玩弄尺寸类。我对图像设置了一些约束,使其根据与 iPhone 纵向和横向对应的尺寸类别处于不同的位置。

The constraints are visible in the images below. As you can see, when I am in Compact/Compact, some constraints are "installed" while others are not, and in Compact/Regular, other constraints are installed.

约束在下图中可见。如您所见,当我在 Compact/Compact 中时,一些约束被“安装”而其他约束没有,而在 Compact/Regular 中,其他约束被安装。

Compact/CompactCompact/Regular

紧凑型/紧凑型紧凑型/常规型

However, I get an error (slightly visible in both screenshots) basically claiming that my uninstalled constraints are interfering with my installed constraints (which, if they were run at the same time, is true) but shouldn't the uninstalled constraints for each size class not even be run? None of the installed constraints cause issues with each other.

但是,我收到一个错误(在两个屏幕截图中都略微可见)基本上声称我卸载的约束干扰了我安装的约束(如果它们同时运行,则为真)但不应该每个大小的卸载约束连课都跑不掉?安装的约束都不会导致彼此出现问题。

The error that shows up is:

出现的错误是:

2014-06-30 21:14:26.215 TV Tracker[26417:1434692] SetAppThreadPriority: setpriority failed with error 45
2014-06-30 21:14:26.372 TV Tracker[26417:1434692] 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:0x10bb11da0 H:|-(24)-[UILabel:0x10bb4db70'TV Tracker uses your Trak...']   (Names: '|':UIControl:0x10bb2db60 )>",
"<NSLayoutConstraint:0x10bb11df0 H:[UIImageView:0x10bb44740]-(>=18)-[UILabel:0x10bb4db70'TV Tracker uses your Trak...']>",
"<NSLayoutConstraint:0x10bb2f420 H:|-(20)-[UIButton:0x10bb35470'Sign In']   (Names: '|':UIControl:0x10bb2db60 )>",
"<NSLayoutConstraint:0x10bb2f470 UIButton:0x10bb35470'Sign In'.leading == UIImageView:0x10bb44740.leading>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x10bb11df0 H:[UIImageView:0x10bb44740]-(>=18)-[UILabel:0x10bb4db70'TV Tracker uses your Trak...']>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2014-06-30 21:14:26.373 TV Tracker[26417:1434692] 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:0x10bb11e90 H:[UILabel:0x10bb4db70'TV Tracker uses your Trak...']-(23)-|   (Names: '|':UIControl:0x10bb2db60 )>",
"<NSLayoutConstraint:0x10bb11ee0 UILabel:0x10bb4db70'TV Tracker uses your Trak...'.trailing == UIButton:0x10bb44ec0'Register'.trailing>",
"<NSLayoutConstraint:0x10bb2f510 H:[UIButton:0x10bb44ec0'Register']-(20)-|   (Names: '|':UIControl:0x10bb2db60 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x10bb11ee0 UILabel:0x10bb4db70'TV Tracker uses your Trak...'.trailing == UIButton:0x10bb44ec0'Register'.trailing>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2014-06-30 21:14:26.388 TV Tracker[26417:1434692] 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) 
(
"<NSAutoresizingMaskLayoutConstraint:0x10bb49e10 h=-&- v=-&- UIControl:0x10bb2db60.width == UIViewControllerWrapperView:0x10d102000.width>",
"<NSAutoresizingMaskLayoutConstraint:0x10bb55d10 h=-&- v=-&- UIViewControllerWrapperView:0x10d102000.width == UINavigationTransitionView:0x10bc6f770.width>",
"<NSAutoresizingMaskLayoutConstraint:0x10bb3cda0 h=-&- v=-&- UINavigationTransitionView:0x10bc6f770.width == UILayoutContainerView:0x10bc6e3e0.width>",
"<NSAutoresizingMaskLayoutConstraint:0x10bb29960 h=-&- v=-&- UILayoutContainerView:0x10bc6e3e0.width == UITransitionView:0x10bc73670.width>",
"<NSAutoresizingMaskLayoutConstraint:0x10bb559e0 h=-&- v=-&- UITransitionView:0x10bc73670.width == UIWindow:0x10d068e70.width>",
"<NSAutoresizingMaskLayoutConstraint:0x10bb56880 h=--- v=--- H:[UIWindow:0x10d068e70(320)]>",
"<NSLayoutConstraint:0x10bb2f420 H:|-(20)-[UIButton:0x10bb35470'Sign In']   (Names: '|':UIControl:0x10bb2db60 )>",
"<NSLayoutConstraint:0x10bb2f470 UIButton:0x10bb35470'Sign In'.leading == UIImageView:0x10bb44740.leading>",
"<NSLayoutConstraint:0x10bb44570 UIControl:0x10bb2db60.centerX == UIImageView:0x10bb44740.centerX>",
"<NSLayoutConstraint:0x10bb3c840 H:[UIImageView:0x10bb44740(92)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x10bb3c840 H:[UIImageView:0x10bb44740(92)]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2014-06-30 21:14:26.389 TV Tracker[26417:1434692] 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) 
(
"<_UILayoutSupportConstraint:0x10bd1c8a0 V:[_UILayoutGuide:0x10bb4acb0(64)]>",
"<_UILayoutSupportConstraint:0x10bd1fbf0 V:|-(0)-[_UILayoutGuide:0x10bb4acb0]   (Names: '|':UIControl:0x10bb2db60 )>",
"<NSAutoresizingMaskLayoutConstraint:0x10bb56d40 h=-&- v=-&- UIControl:0x10bb2db60.height == UIViewControllerWrapperView:0x10d102000.height>",
"<NSAutoresizingMaskLayoutConstraint:0x10bb56c60 h=-&- v=-&- UIViewControllerWrapperView:0x10d102000.height == UINavigationTransitionView:0x10bc6f770.height>",
"<NSAutoresizingMaskLayoutConstraint:0x10bb3cbb0 h=-&- v=-&- UINavigationTransitionView:0x10bc6f770.height == UILayoutContainerView:0x10bc6e3e0.height>",
"<NSAutoresizingMaskLayoutConstraint:0x10bb4a920 h=-&- v=-&- UILayoutContainerView:0x10bc6e3e0.height == UITransitionView:0x10bc73670.height>",
"<NSAutoresizingMaskLayoutConstraint:0x10bb0e3a0 h=-&- v=-&- UITransitionView:0x10bc73670.height == UIWindow:0x10d068e70.height>",
"<NSAutoresizingMaskLayoutConstraint:0x10bb44d20 h=--- v=--- V:[UIWindow:0x10d068e70(568)]>",
"<NSLayoutConstraint:0x10bb44520 UIControl:0x10bb2db60.centerY == UIImageView:0x10bb44740.centerY>",
"<NSLayoutConstraint:0x10bb445c0 V:[_UILayoutGuide:0x10bb4acb0]-(24)-[UIImageView:0x10bb44740]>",
"<NSLayoutConstraint:0x10bb49b60 V:[UIImageView:0x10bb44740(100)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x10bb49b60 V:[UIImageView:0x10bb44740(100)]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2014-06-30 21:14:26.396 TV Tracker[26417:1434692] 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) 
(
"<_UILayoutSupportConstraint:0x10bd13470 V:[_UILayoutGuide:0x10bb4c690(0)]>",
"<_UILayoutSupportConstraint:0x10bd13410 _UILayoutGuide:0x10bb4c690.bottom == UIControl:0x10bb2db60.bottom>",
"<NSAutoresizingMaskLayoutConstraint:0x10bb56d40 h=-&- v=-&- UIControl:0x10bb2db60.height == UIViewControllerWrapperView:0x10d102000.height>",
"<NSAutoresizingMaskLayoutConstraint:0x10bb56c60 h=-&- v=-&- UIViewControllerWrapperView:0x10d102000.height == UINavigationTransitionView:0x10bc6f770.height>",
"<NSAutoresizingMaskLayoutConstraint:0x10bb3cbb0 h=-&- v=-&- UINavigationTransitionView:0x10bc6f770.height == UILayoutContainerView:0x10bc6e3e0.height>",
"<NSAutoresizingMaskLayoutConstraint:0x10bb4a920 h=-&- v=-&- UILayoutContainerView:0x10bc6e3e0.height == UITransitionView:0x10bc73670.height>",
"<NSAutoresizingMaskLayoutConstraint:0x10bb0e3a0 h=-&- v=-&- UITransitionView:0x10bc73670.height == UIWindow:0x10d068e70.height>",
"<NSAutoresizingMaskLayoutConstraint:0x10bb44d20 h=--- v=--- V:[UIWindow:0x10d068e70(568)]>",
"<NSLayoutConstraint:0x10bb11e40 UIControl:0x10bb2db60.centerY == UILabel:0x10bb4db70'TV Tracker uses your Trak...'.centerY + 0.25>",
"<NSLayoutConstraint:0x10bb2f3d0 V:[UILabel:0x10bb4db70'TV Tracker uses your Trak...']-(91)-[_UILayoutGuide:0x10bb4c690]>",
"<NSLayoutConstraint:0x10bb2d790 V:[UILabel:0x10bb4db70'TV Tracker uses your Trak...'(69)]>"
)

Will attempt to recover by breaking constraint 

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

My sidebar for the Compact/Compact constraints look like C/C sidebar

我的 Compact/Compact 约束的侧边栏看起来像 C/C侧边栏

My sidebar for the Compact/Regular constraints look like C/R sidebar

我的 Compact/Regular 约束的侧边栏看起来像 C/R 侧边栏

My view hierarchy for this login controller: view hierarchy

我这个登录控制器的视图层次结构: 视图层次结构

回答by matt

I had the same problem the first time I tried this feature, until I realized what the "+" buttons were telling me. At first, I had changed size class and deleted a view and added a view, or deleted a constraint and added a constraint. That is wrong! You cannot delete or add views or constraints on a per-size-class basis; they must allbe there in every size class — only some of them are marked "installed" for that size class, and others are not.

我第一次尝试这个功能时遇到了同样的问题,直到我意识到“+”按钮告诉我什么。一开始,我改变了size class,删除了一个view再添加了一个view,或者删除了一个constraint再添加了一个constraint。那是错的!您不能在每个尺寸类别的基础上删除或添加视图或约束;他们必须在那里每一个尺寸级别-只有一些被标记为“已安装”对于尺寸级别,而有些则没有。

Here's some further advice.

这里有一些进一步的建议。

  • Look at the top of your screen shot. You are getting warnings from Interface Builder - and ignoring them. Don't ignore them! Those warnings are telling you that your constraints are not doing what you think they are.

  • Show the view hierarchy list at the left of the canvas (it is hidden in your screen shots) and keep looking at and working on every combination of size class until there are no more warnings. It also tells you what constraints and views are present in the mass case and for the particular size class case you are looking at (see screen shot below).

  • Use the "+" buttons in the attributes inspector on the right to make certain that you have values for every relevant size class case. You do not add or remove constraints or classes: you add "installed" buttons and mark their size class and check or uncheck them (again, see screen shot).

  • 查看屏幕截图的顶部。您从 Interface Builder 收到警告 - 并忽略它们。不要忽视他们!这些警告告诉您,您的约束并没有按照您的想法行事。

  • 在画布左侧显示视图层次列表(它隐藏在您的屏幕截图中)并继续查看和处理大小类的每个组合,直到不再有警告。它还告诉您在质量案例以及您正在查看的特定尺寸类案例中存在哪些约束和视图(请参见下面的屏幕截图)。

  • 使用右侧属性检查器中的“+”按钮确保您拥有每个相关尺寸类案例的值。您不添加或删除约束或类:您添加“已安装”按钮并标记它们的大小类并选中或取消选中它们(再次参见屏幕截图)。

enter image description here

在此处输入图片说明

Finally, use the Preview Assistant to make certain the view will look the way you expect. See cute animation below.

最后,使用预览助手确保视图看起来像您期望的那样。请看下面的可爱动画。

enter image description here

在此处输入图片说明

回答by Bharath Vankireddy

For example if you can notice first warning from your log -(24)-[UILabel:0x10bb4db70'TV Tracker uses your Trak...'] Its clearly says that there is a constraint which is added by you is not required. Take a example like this, If you added the top space and bottom space for any of the object you no need to set the height for that object because that object will expand based on the top and bottom spaces provided by you. Like the same way your log also says that UILable don't require the height/width/x/y which is equal to 24. Please find it and remove from storyboard that will resolve your issue.

例如,如果您可以从日志中注意到第一个警告 -(24)-[UILabel:0x10bb4db70'TV Tracker uses your Trak...'] 它清楚地表明您添加的约束不是必需的。以这样的例子为例,如果您为任何对象添加了顶部空间和底部空间,则无需设置该对象的高度,因为该对象将根据您提供的顶部和底部空间进行扩展。就像你的日志也说 UILable 不需要等于 24 的高度/宽度/x/y 一样。请找到它并从故事板中删除,这将解决你的问题。