Calculate the contentsize of scrollview

前端 未结 7 1432
旧巷少年郎
旧巷少年郎 2020-12-13 11:40

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

相关标签:
7条回答
  • 2020-12-13 11:57
    scrollView.contentSize = [scrollView sizeThatFits:scrollView.frame.size]
    

    I believe would also work

    0 讨论(0)
  • 2020-12-13 11:59

    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

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2020-12-13 12:13

    I had the same situation, but then I wrote a new version in Swift 4 mirroring the better answer in Objective-C by Hannes Larsson:

    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 讨论(0)
  • 2020-12-13 12:16

    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.

    0 讨论(0)
  • 2020-12-13 12:18

    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.

    0 讨论(0)
提交回复
热议问题