問題描述
我有:
- 應用程序,應該可以在橫向和縱向模式下運行.
- 頂部帶有全尺寸滾動視圖的視圖.
- 滾動視圖內的一些視圖,具有固定的寬度和高度.(添加了 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 尺寸的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!