根据子视图的内容大小调整滚动视图
Adjusting scroll view depending on content size of subview
我正在尝试允许滚动视图根据此页面上子视图的大小调整大小。图片中对象的所有其他约束都可以正常工作。
当我向我的子视图添加约束以拥抱屏幕的左侧、右侧和底部(并且子视图的顶部拥抱分段控件的底部)然后更新帧时,它使高度子视图 0.
我无法向子视图添加高度限制,因为子视图的大小会根据其中的内容量动态变化,这将改变滚动视图的大小。
如何在不添加高度限制的情况下完成此操作?
您的视图层次结构应如下所示:
Create a single child view of the UIScrollView where we will put all
of our content
The content view has to be an explicit size (or a placeholder size in
interface builder and set at run time). In other words your content
view cannot depend on the scroll view to get its size. It can,
however, depend on views outside of the scroll view to get its size.
来自 Apple 技术说明 TN2154
Set translatesAutoresizingMaskIntoConstraints to NO on all views
involved.
Position and size your scroll view with constraints external
to the scroll view.
Use constraints to lay out the subviews within the
scroll view, being sure that the constraints tie to all four edges of
the scroll view and do not rely on the scroll view to get their size.
尝试为您的子视图提供 Equl Heights
和 Equal Width
约束。
我正在尝试允许滚动视图根据此页面上子视图的大小调整大小。图片中对象的所有其他约束都可以正常工作。
当我向我的子视图添加约束以拥抱屏幕的左侧、右侧和底部(并且子视图的顶部拥抱分段控件的底部)然后更新帧时,它使高度子视图 0.
我无法向子视图添加高度限制,因为子视图的大小会根据其中的内容量动态变化,这将改变滚动视图的大小。
如何在不添加高度限制的情况下完成此操作?
您的视图层次结构应如下所示:
Create a single child view of the UIScrollView where we will put all of our content
The content view has to be an explicit size (or a placeholder size in interface builder and set at run time). In other words your content view cannot depend on the scroll view to get its size. It can, however, depend on views outside of the scroll view to get its size.
来自 Apple 技术说明 TN2154
Set translatesAutoresizingMaskIntoConstraints to NO on all views involved.
Position and size your scroll view with constraints external to the scroll view.
Use constraints to lay out the subviews within the scroll view, being sure that the constraints tie to all four edges of the scroll view and do not rely on the scroll view to get their size.
尝试为您的子视图提供 Equl Heights
和 Equal Width
约束。