Calculate scroll content

I have scrollview as a detailed view of a tableview cell. There are several notions of detailed viewing, such as shortcuts, buttons, etc., which I create through the interface constructor. What I create through the interface constructor is static. I put everything in a height view of 480.

The label for my detailed view has dynamic text that can extend to any length. The problem is that I need to set the size of the scroll content, for which I need its height.

How to set scroll height if content is dynamic?

+7
iphone uiscrollview
source share
7 answers

You can try using ContentSize to scroll. 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 viewWillAppear method in the controller for a view that contains scrollview. This is the last thing I do before calling viewDidLoad in super.

Hope this solves your problem.

// Hannes

+20
source share

Correct the 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 these are only sums of heights, for example. if there are two sub-items side by side, their heights should be added, making the amount larger than it should be. In addition, if there are vertical gaps between the subzones, the amount will be less than it should be. Incorrect height confuses scrollRectToVisible by providing random scroll positions :)

This loop works and is being 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; } 
+8
source share

A slightly simpler way to do this is to insert the layout into the view, and then put that view into the scrollview. Assuming you are using 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, longView knows how tall it is, and the contents of scrollview are simply set.

+1
source share

It depends on the type of content you want to add dynamically. So, let's say you have big text data to display, then use a UITextView and, since it is a subclass of UIScrollView, you can get setContentSize TextView when assigning text content. Based on this, you can set the overall 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 the text and its end. Thus, you can create a shortcut or other TextView and display your text there. You can add these components as a subview in scrollview.

 UITextView *myDisplayContent = [[UITextView alloc] initWithFrame:CGRectMake(0.0f, yPoint, 300.f, calculatorTextView.contentSize.height)]; myDisplayContent.text = @"My lengthy text ...."; [myScrollView addSubview:myDisplayContent]; 

// Finally, set the content size of "myScrollView" to the total length of the display area.

 [myScrollView setContentSize:yPoint + heightOfLastComponent]; 

This works for me.

+1
source share

I assume that there is no automatic in the case of scrollview, and the content should be calculated for static views on the screen, at least, and for dynamic, as soon as it should be calculated on the go.

0
source share
 scrollView.contentSize = [scrollView sizeThatFits:scrollView.frame.size] 

I believe that will also work

0
source share

I had the same situation, but then I wrote a new version in Swift 4 reflecting the best answer in Hannes Larsson's Objective-C:

 import UIKit extension UIScrollView { func fitSizeOfContent() { let sumHeight = self.subviews.map({$0.frame.size.height}).reduce(0, {x, y in x + y}) self.contentSize = CGSize(width: self.frame.width, height: sumHeight) } } 
0
source share

All Articles