简体   繁体   中英

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.

伊姆古尔

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

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

  • 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.

    UIScrollView And Autolayout

    Using UIScrollView with Auto Layout in iOS

Try giving the Equl Heights & Equal Width constraints to your Subview.

宽度相等和高度相等

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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