问题描述
我有:
- 应用程序,应该可以在横向和纵向模式下运行.
- 顶部带有全尺寸滚动视图的视图.
- 滚动视图内的一些视图,具有固定的宽度和高度.(添加了 H 和 W 约束)
- 设置为在容器中水平居中查看滚动视图.(根据约束添加)
我在界面生成器中收到警告具有不明确的可滚动内容宽度".
据我所知,解决此问题的唯一方法是设置尾随和前导约束.但对于不同的 iPhone(5.5"、4.7"、4"),我需要设置不同的尾随和前导约束.
我怎样才能消除这个警告,并且仍然在所有 iPhone 尺寸的固定 W 和 H 的情况下保持居中的水平视图?
我创建 Github repo 来说明这个问题:
附言
我不知道它为什么起作用以及 Xcode 如何检测哪个约束的优先级更高(因为我没有明确为这些约束设置优先级),但如果有人解释,我将不胜感激,为什么它在下面的评论中起作用.
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)
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.
How can I eliminate this warning and still have centered horizontally view with fixed W and H for all iPhone sizes?
I create Github repo to illustrate this problem: 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.
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.
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)
Important: you have to add trailing and/or bottom constraints. Not "leading and top" - it's not works!
You can check it in my example project, that demonstrating how to fix this issue: 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.
这篇关于UIScrollView + 居中视图 + 模糊的可滚动内容大小 + 许多 iPhone 尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!