Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate the contentsize of scrollview

I'm having a scrollview as the detailedview of tableview cell. There are multiple views on the detailedview like labels, buttons etc. which I'm creating through interface builder. What I'm creating through interface builder is static. I'm putting everything on a view of height 480.

A label on my detailedview is having dynamic text which can extend to any length. The problem is that I need to set the scrollview's content size for which I need its height.

How shall I set scrollview's height provided the content is dynamic?

like image 574
neha Avatar asked Jun 15 '10 15:06

neha


People also ask

What is Contentsize of ScrollView?

The content size of a scroll view doesn't change anything about the bounds of a scroll view and therefore does not impact how a scroll view composites its subviews. Instead, the content size defines the scrollable area. By default, a scroll view's content size is a big, fat {w:0, h:0} .

What is contentOffset in ScrollView?

contentOffset is where the user is standing after scrolling the area. So this would change each and every time the user scrolls up and down. At times this can be set programmatically as well as in main thread, which will scroll up to given value if the position exists.

How do I get ScrollView height in react native?

Get the height of the contents within the ScrollView ("contentHeight") Use the ScrollView's "onScroll" event to get the scroll offset. Use it to update the scrollOffset animation. Create a ScrollBar component based on the scrollOffset animation value, containerHeight, and contentHeight.

What is use of ScrollView content size method Swift?

Overview. Scroll views allow you to display content in an area on the screen that is smaller than the size of the content. They allow the user to pan (scroll) and/or zoom the content. The UITableView and UICollectionView are a subclasses of UIScrollView , so many of the points below will apply to them as well.


2 Answers

You could try to use the scrollview'ers ContentSize. It worked for me and I had the same problem with the control using dynamic content.

    // Calculate scroll view size
float sizeOfContent = 0;
int i;
for (i = 0; i < [myScrollView.subviews count]; i++) {
    UIView *view =[myScrollView.subviews objectAtIndex:i];
    sizeOfContent += view.frame.size.height;
}

// Set content size for scroll view
myScrollView.contentSize = CGSizeMake(myScrollView.frame.size.width, sizeOfContent);

I do this in the method called viewWillAppear in the controller for the view that holds the scrollview. It is the last thing i do before calling the viewDidLoad on the super.

Hope it will solve your problem.

//hannes

like image 92
Hannes Larsson Avatar answered Nov 11 '22 09:11

Hannes Larsson


Correct shorter example:

float hgt=0; for (UIView *view in scrollView1.subviews) hgt+=view.frame.size.height;

[scrollView1 setContentSize:CGSizeMake(scrollView1.frame.size.width,hgt)];

Note that this only sums heights, e.g. if there are two subviews side by side their heights with both be added, making the sum greater than it should be. Also, if there are vertical gaps between the subviews, the sum will be less than it should be. Wrong height confuses scrollRectToVisible, giving random scroll positions :)

This loop is working and tested:

float thisy,maxy=0;for (UIView *view in scrollView1.subviews) {
    thisy=view.frame.origin.y+view.frame.size.height; maxy=(thisy>maxy) ? thisy : maxy;
}
like image 29
Henrik Erlandsson Avatar answered Nov 11 '22 11:11

Henrik Erlandsson