Calculate the contentsize of scrollview

2019-01-21 21:06发布

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?

6条回答
\"骚年 ilove
2楼-- · 2019-01-21 21:23

A somewhat easier way to do this is to nest your layout within a view then put that view within the scrollview. Assuming you use tags, this works as follows:

UIScrollView *scrollview = (UIScrollView *)[self.view viewWithTag:1];
UIView *longView = (UIView *)[self.view viewWithTag:2];
scrollview.contentSize = CGSizeMake(scrollView.frame.size.width, longView.frame.size.height);

That way the longView knows how tall it is, and the scrollview's content is just set to match.

查看更多
趁早两清
3楼-- · 2019-01-21 21:27

This depends on the type of content you are going to add dynamically. So let's say you have a big text data to show, then use the UITextView and as it is a subclass of the UIScrollView, you can get the setContentSize of TextView when you assign the text content. Based on that you can set the total size of the UIScrollView.

float yPoint = 0.0f; 
UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, yPoint, 320.0f, 400.0f)];

UITextView *calculatorTextView = [[UITextView alloc] init]; calculatorTextView.text = @"My looong content text ..... this has a dynamic content"; `


[calculatorTextView sizeToFit];


yPoint = yPoint + calculatorTextView.contentSize.height; // Bingo, we have the new yPoint now to start the next component.

// Now you know the height of your text and where it will end. So you can create a Label or another TextView and display your text there. You can add those components as subview to the scrollview.

UITextView *myDisplayContent = [[UITextView alloc] initWithFrame:CGRectMake(0.0f, yPoint, 300.f, calculatorTextView.contentSize.height)];

myDisplayContent.text = @"My lengthy text ....";
[myScrollView addSubview:myDisplayContent];

// At the end, set the content size of the 'myScrollView' to the total length of the display area.

[myScrollView setContentSize:yPoint + heightOfLastComponent];

This works for me.

查看更多
你好瞎i
4楼-- · 2019-01-21 21:32

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

查看更多
劳资没心,怎么记你
5楼-- · 2019-01-21 21:40

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;
}
查看更多
唯我独甜
6楼-- · 2019-01-21 21:45
scrollView.contentSize = [scrollView sizeThatFits:scrollView.frame.size]

I believe would also work

查看更多
【Aperson】
7楼-- · 2019-01-21 21:49

I guess there's no auto in case of scrollview, and the contentsize should be calculated for static views on the screen at least and for dynamic once it should be calculated on the go.

查看更多
登录 后发表回答