Calculate the contentsize of scrollview

前端 未结 7 1433
旧巷少年郎
旧巷少年郎 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 12:20

    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.

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