简体   繁体   English

更改设备方向时违反iOS自动布局约束

[英]iOS autolayout constraint violation when device orientation is changed

I have an autolayout layout that has been created for an iPhone in portrait mode. 我有一个为人像模式下的iPhone创建的自动版式布局。

When I rotate the device from portrait to landscape I get a warning saying the system cannot satisfy two constraints at the same time. 当我将设备从纵向旋转到横向时,会收到一条警告,提示系统无法同时满足两个约束条件。 This happens when rotating to and from both orientation. 双向旋转时会发生这种情况。

在此处输入图片说明

I believe I have found the offending constraint (highlighted below), and it is one generated by iOS, moving my constraint that is in conflict breaks my layout and causes more constraint violation errors. 我相信我已经发现了令人讨厌的约束(下面突出显示),它是iOS生成的约束,移动处于冲突状态的约束会破坏我的布局,并导致更多的违反约束的错误。

The layout works visually; 布局外观直观; so long as I ignore the warning. 只要我忽略警告。

The layout consists of two basic parts, a control parts (using a UIStackView that is 30% of the height of the device) and a UITableView that fills the remaining area. 布局由两个基本部分组成,一个是控制部分(使用的是设备高度的30%的UIStackView ),另一个是填充剩余区域的UITableView These are embedded in a UIStackView that is set to cover the default UIView with margins to the safe area. 这些嵌入在UIStackView ,该UIStackView设置为覆盖默认UIView并在安全区域UIStackView空白。

在此处输入图片说明

To make best use of the the available screen space when the device is rotated to landscape the control part should go from above the UITableView to beside it, and take up 45% of the width of the screen. 为了在设备旋转以横向显示时充分利用可用的屏幕空间,控制部件应从UITableView上方移至其旁边,并占据屏幕宽度的45%。

The conflicting constraint is on the "controller" part of the view setting its leading edge to be 16 points from the superviews leading edge. 冲突约束位于视图的“控制器”部分,将其前沿设置为距超级视图前沿16个点。

Is there away to get around this? 有没有解决的办法? I don't like too, but can the warning be ignored? 我也不喜欢,但是警告可以忽略吗?

I have tried embedding the main UIStackView in a UIView and removing the constraint that is in conflict; 我已经尝试将主UIStackView嵌入UIView并删除冲突的约束。 but his causes other issues unless I add in constraints between the UIView and UIStackView which then have the same conflicting constraints issue. 但是他会引起其他问题,除非我在UIViewUIStackView之间添加约束,然后它们具有相同的冲突约束问题。

A repo for an example project with the same issue can be found at . 一种具有相同问题的示例项目回购可以发现

When simplifying the layout it appears I am unable to have a UIStackView with Alignment = Fill , Distribution = Fill Equally and Spacing = 5 (or standard spacing) as the spacing on the UIStackView appears to be. 当简化布局时,由于UIStackView上的Spacing = 5看起来似乎无法使用Alignment = FillDistribution = Fill Equally UIStackViewSpacing = 5 (或标准间距)的UIStackView in conflict. 发生冲突。

It's common to see these types of constraint warnings when using Vary for Traits. 在使用“变化特性”时,通常会看到这些类型的约束警告。

You can get rid of the constraint conflicts on rotation by changing the Priority on both of your proportional constraints on Info Stack View to 999 . 通过将“ Info Stack View上两个比例约束的“ Priority ”都更改为999可以摆脱旋转约束的冲突。

When I run your Experiment app, I also see a constraint conflict on your Detail Stack View ... but, it looks like an internal conflict that you may not be able to get rid of. 当我运行实验应用程序时,我还在“ Detail Stack View上看到了约束冲突……但是,它看起来像是内部冲突,您可能无法摆脱。 Changing that stack view's Distribution to Fill Equally instead of Fill Proportionally clears it, but that may not give you the exact layout you're looking for. 将该堆栈视图的“ Distribution更改为“ Fill Equally而不是“ Fill Proportionally清除它,但这可能无法为您提供所需的确切布局。

Fill Proportionally seems to be the most misunderstood and misused setting... I'd suggest modifying your layout to avoid that... Probably by embedding Detail Stack View in a UIView , Distribution: Fill , and constraining it to be centered in that UIView . Fill Proportionally似乎是最容易理解和滥用的设置...我建议您修改布局以避免这种情况...可能是通过将Detail Stack View嵌入UIViewDistribution: Fill并将其约束在该UIView居中。

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

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