简体   繁体   English

根据子视图的内容大小调整滚动视图

[英]Adjusting scroll view depending on content size of subview

I'm trying to allow the scroll view to adjust size depending on the size of a subview on this page. 我试图允许滚动视图根据此页面上子视图的大小来调整大小。 All of the other constraints for the objects in the picture work fine. 图片中对象的所有其他约束都可以正常工作。

伊姆古尔

When I add constraints to my subview to hug to the left, right, and bottom of the screen (and the top of the subview hugs to the bottom of the segmented control) and then update frames, it makes the height of the subview 0. 当我向子视图添加约束以拥抱到屏幕的左,右和底部(子视图的顶部拥抱到分段控件的底部)然后更新框架时,它将使子视图的高度为0。

伊姆古尔

I can't add a height constraint to the subview because the size of the subview will change dynamically depending on the amount of content in it, which will then change the size of the scroll view. 我不能在子视图中添加高度限制,因为子视图的大小将根据其中的内容量动态变化,然后将更改滚动视图的大小。

How can I accomplish this without adding a height constraint? 如何在不增加高度限制的情况下完成此任务?

Your view hierarchy should look like : 您的视图层次结构应如下所示:

视图层次

Create a single child view of the UIScrollView where we will put all of our content 创建UIScrollView的单个子视图,将所有内容放入其中

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. 但是,它可以依赖于滚动视图之外的视图来获取其大小。

From Apple Technical Note TN2154 从Apple 技术说明TN2154

  • Set translatesAutoresizingMaskIntoConstraints to NO on all views involved. 在所有涉及的视图上将translatesAutoresizingMaskIntoConstraints设置为NO。

  • 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. 使用约束在滚动视图中布置子视图,确保约束与滚动视图的所有四个边缘相关联,并且不依赖于滚动视图来获取其大小。

    UIScrollView And Autolayout UIScrollView和自动布局

    Using UIScrollView with Auto Layout in iOS 在iOS中将UIScrollView与自动布局一起使用

Try giving the Equl Heights & Equal Width constraints to your Subview. 尝试将“ Equl Heights和“ Equal Width约束条件赋予子视图。

宽度相等和高度相等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM