ios UIScrollView + Centered View + Ambigous Scrollable Content Size + 许多 iPhone 尺寸
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26261659/
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
UIScrollView + Centered View + Ambigous Scrollable Content Size + many iPhone sizes
提问by skywinder
I have:
我有:
- application, that should work in landscape and portrait mode.
- view with full-size scroll view on top.
- some view inside scroll view with fixed width and height. (with added H and W constraints)
- set to view inside scroll view as horizontal centered in container. (added according constraint)
- 应用程序,应该在横向和纵向模式下工作。
- 顶部带有全尺寸滚动视图的视图。
- 滚动视图中的一些视图具有固定的宽度和高度。(添加了 H 和 W 约束)
- 设置为在容器中水平居中查看滚动视图。(根据约束添加)
I have warning in interface builder "Has ambiguous scrollable content width".
我在界面构建器中发出警告“具有不明确的可滚动内容宽度”。
The only way to fix this problem, that I know - is set trailing and leading constraints. But for different iPhones (5.5", 4.7", 4") I need to set different trailing and leading constraints.
我知道,解决这个问题的唯一方法是设置尾随和前导约束。但是对于不同的 iPhone(5.5"、4.7"、4"),我需要设置不同的尾随和前导约束。
How can I eliminate this warning and still have centered horizontally view with fixed W and H for all iPhone sizes?
我怎样才能消除这个警告,并且对于所有 iPhone 尺寸仍然具有固定 W 和 H 的水平居中视图?
I create Github repo to illustrate this problem: ScrollViewAmbigous
我创建了 Github repo 来说明这个问题:ScrollViewAmbigous
This is not duplicate of UIScrollView Scrollable Content Size Ambiguity, but it similar (and not answered although), but this question especially related to different sizes of iPhones.
这不是UIScrollView Scrollable Content Size Ambiguity 的重复 ,但它相似(虽然没有回答),但这个问题尤其与不同尺寸的 iPhone 相关。
回答by skywinder
In the morning with a fresh cup of coffee I figured out workaround for this issue!
早上喝一杯新鲜的咖啡,我想出了解决这个问题的方法!
So, here is the initial state for simplest case:
所以,这是最简单情况的初始状态:
- scrollView with 0 constraints to all edges
- Button centered Horizontal and Vertical with fixed Width and Height
- And, of course
Has ambiguous scrollable content width
andHas ambiguous scrollable content height
annoying warnings.
- 对所有边缘具有 0 约束的 scrollView
- 按钮居中水平和垂直,具有固定的宽度和高度
- 而且,当然
Has ambiguous scrollable content width
还有Has ambiguous scrollable content height
烦人的警告。
All, that we have to do is:
我们要做的就是:
- Add 2 additional constraints, for example "0" for trailing and/or bottom space for our view (in my case - UIButton)
- 添加 2 个额外的约束,例如“0”用于我们视图的尾随和/或底部空间(在我的情况下 - UIButton)
Important:you have to add trailing and/or bottomconstraints. Not "leading and top" - it's not works!
重要提示:您必须添加尾随和/或底部约束。不是“领先和顶级” - 这是行不通的!
You can check it in my example project, that demonstrating how to fix this issue: ScrollViewAmbigous
您可以在我的示例项目中查看它,该项目演示了如何解决此问题:ScrollViewAmbigous
P.S.
聚苯乙烯
I don't know why it works and how Xcode detect which constraint is more prioritised (because I'm not set priority for these constraints explicity), but I'll be thankful if someone explain, why it works in comments below.
我不知道它为什么起作用以及 Xcode 如何检测哪个约束的优先级更高(因为我没有明确地为这些约束设置优先级),但如果有人解释,我会很感激,为什么它在下面的评论中起作用。
回答by Timur X.
Problem:
问题:
- The warning is telling us that the the content size of the scrollview depends on its subviews.
- while your subview don't have any strict rule of position and size (no fixed constraints) it will confuse nib generated scrollview content size.
- 警告告诉我们滚动视图的内容大小取决于其子视图。
- 虽然您的子视图没有任何严格的位置和大小规则(没有固定约束),但它会混淆笔尖生成的滚动视图内容大小。
Solution:
解决方案:
- Make only one subview as a 'contentView' for scrollview.
Add strict (fixed) constraints to that 'contentView'.
Best practice : equal width and height to its scroll view.
- Add all other subviews and constraints to your 'contentView'.
- 仅将一个子视图作为滚动视图的“contentView”。
向该“contentView”添加严格(固定)约束。
最佳实践:与其滚动视图的宽度和高度相等。
- 将所有其他子视图和约束添加到您的“contentView”。
回答by Reuben Scratton
There seems to be a lot of confusion on this issue. My take is that a UIScrollView must have TWOtrailing space constraints, an 'inner' one connecting it to one of it's subviews (so it can know its content width), and another 'outer' one connecting it to a sibling or superview so it knows its frame width.
在这个问题上似乎有很多困惑。我的看法是 UIScrollView 必须有两个尾随空间约束,一个“内部”一个将它连接到它的一个子视图(这样它可以知道它的内容宽度),另一个“外部”将它连接到一个兄弟视图或超级视图,所以它知道它的框架宽度。
Same principle applies for height, i.e. two bottom space constraints.
相同的原则适用于高度,即两个底部空间限制。