简体   繁体   中英

Size a UIView to fit all of its contents in a uiScrollView

I am having a problem sizing a uiView inside of my uiScrollView to contain all of its subviews. Here is am image of my layout (I shaded the UIView within the scroll view blue to highlight the problem)

my layout

Here is what happens, though when I launch the app. This is what is rendered

rendered view

As you can see the blue content view is only sized to the screen dimensions of the phone, and all subviews below that are outside of the frame and I cannot interact with them. Can anyone tell me how scale this content view properly? This is an older app and I cannot use auto-layouts, only springs and struts.

For Autolayout with UIScrollView There is a Special Technique that we should follow.

  • Create a UIView That you want to be scrollable. And set you constraints according to that.
  • Inser a UIScrollView inside this view with the same size and set the constraints 'Top, Bottom, Leading and Trailing equals to 0'
  • Then insert an other View with the same size in UIScrollView And set the constraints 'Top, Bottom, Leading and Trailing equals to 0'. This will still show red (which means constraints are still not correct) So Control + drag to the parent view of ScrollView that we have created in beginning and set equal width and equal height. Select the equal height constraint and decrease the priority to 750 in the property attribute on the right side of Xcode.
  • Now add the final view inside this view as Content View Insert all the Views inside it and may be this view can be extended height wise more than its parent view, Lets say previous View has 550 Height and this view now has 990 height thats ok. Just insert until you finish adding all contents when you finish just add constraints (Top, Bottom, Leading, Trailing and Height) This is a confusing point. According to above height maybe your bottom constraint has the constant value = -440 and height constraint has 990 just select the bottom constraint and change the value to 0.

Note: You must have change the value of bottom constraint after adding constraint not while adding constraint.

For ManualLayout You can simply use to get the height and y position of you last entered object.

Lets say you last added object is a UIView so just add this code.

self.scrollView.constentSize = CGSizeMake(self.scrollView.frame.size.width + (lastAddedView.frame.origin.y + lastAddedView.frame.size.height /* Here you can add some more space if you feel neccessory*/));

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