使用 AutoLayout 调整 UIScrollView 内的 UIView 大小以进行滚动?
Resize UIView inside UIScrollView with AutoLayout for scrolling?
我的布局如下(使用AutoLayout,iPad):
UIScrollView
将超级视图的 Top、Bottom、Leading、Trailing space 设置为 0。UIView
也具有所有四个类似的约束。第二个 UILabel
以编程方式将其高度设置为 0(因为此处显示了超过一屏的文本)。还有,这4个pin constraints从UIView
到UIScrollView
.
也都设置好了
加载 XIB 后,UILabel
可以正确显示长行文本。但是滚动不起作用。这是 运行 应用程序在 iPad 上的屏幕截图:
最外面的视图设置为黄色背景,UIView
设置为灰色背景。如果您在屏幕截图的末尾仔细观察,UILabel
的文本似乎溢出了 UIView
。
我怀疑因为 UIView
不能 "capture" 它的子视图的大小,所以 UIScrollView
也不能计算它的内容大小(如果它的内容滚动条没有理由工作视图仅等于一个屏幕)。为什么即使 UILabel
显然是 UIView
的子视图也会发生这种情况?如果我的怀疑是正确的,如何将 UIView
的高度设置为等于实际内容大小,以便我的 UILabel
正确地 "contained" 在其中?
(不能静态设置 UIView
的高度和宽度)。
注意:在没有 UIView
的情况下测试了 XIB,并且在 UIScrollView
中只有一个 UILabel
(带有长文本)。现在可以滚动了(因为,我想,滚动视图可以计算它的内容大小)。
NB2:我也尝试了 this SO question and this post 中的答案,但我的 UIView
根本不会超出屏幕的尺寸。 (我只关心水平滚动)
AutoLayout 问题很难用文字来解释。我遇到了同样的问题。我在这个视频中找到了解决方案。希望这对你也有帮助。
https://www.youtube.com/watch?v=UnQsFlMGDsI
This video demonstrates how to create a UIScrollView which has some
views on the top of the screen and a view on the bottom of the screen
in Autolayout. UIScrollView content size is automatically adjusted to
the size of the screen or to the size of the content in such way that
on smaller devices scrolling is active and on larger devices scrolling
is not active because it is not needed. UIButton below UITextFields
always stays on the bottom of the screen and everything works on all
iPhone resolutions (iPhone 4 / 5 / 6 / 6 Plus).
我的布局如下(使用AutoLayout,iPad):
UIScrollView
将超级视图的 Top、Bottom、Leading、Trailing space 设置为 0。UIView
也具有所有四个类似的约束。第二个 UILabel
以编程方式将其高度设置为 0(因为此处显示了超过一屏的文本)。还有,这4个pin constraints从UIView
到UIScrollView
.
加载 XIB 后,UILabel
可以正确显示长行文本。但是滚动不起作用。这是 运行 应用程序在 iPad 上的屏幕截图:
最外面的视图设置为黄色背景,UIView
设置为灰色背景。如果您在屏幕截图的末尾仔细观察,UILabel
的文本似乎溢出了 UIView
。
我怀疑因为 UIView
不能 "capture" 它的子视图的大小,所以 UIScrollView
也不能计算它的内容大小(如果它的内容滚动条没有理由工作视图仅等于一个屏幕)。为什么即使 UILabel
显然是 UIView
的子视图也会发生这种情况?如果我的怀疑是正确的,如何将 UIView
的高度设置为等于实际内容大小,以便我的 UILabel
正确地 "contained" 在其中?
(不能静态设置 UIView
的高度和宽度)。
注意:在没有 UIView
的情况下测试了 XIB,并且在 UIScrollView
中只有一个 UILabel
(带有长文本)。现在可以滚动了(因为,我想,滚动视图可以计算它的内容大小)。
NB2:我也尝试了 this SO question and this post 中的答案,但我的 UIView
根本不会超出屏幕的尺寸。 (我只关心水平滚动)
AutoLayout 问题很难用文字来解释。我遇到了同样的问题。我在这个视频中找到了解决方案。希望这对你也有帮助。
https://www.youtube.com/watch?v=UnQsFlMGDsI
This video demonstrates how to create a UIScrollView which has some views on the top of the screen and a view on the bottom of the screen in Autolayout. UIScrollView content size is automatically adjusted to the size of the screen or to the size of the content in such way that on smaller devices scrolling is active and on larger devices scrolling is not active because it is not needed. UIButton below UITextFields always stays on the bottom of the screen and everything works on all iPhone resolutions (iPhone 4 / 5 / 6 / 6 Plus).