Zac24
Zac24

Reputation: 2512

how to set the content size of a scrollView Dynamically

I have a scrollView and on that scrollView, i have many labels and textView along with one tableView. scrollView is working perfectly fine when the no. of rows are less in tablView but if tableView have more number of rows then i am not able to full content and even i am loosing lots of information in tableView because the tableView isn't scrollable. i have added all the lables, textView and tableView as a subview of scrollview. Can anyone help me in that so i can get full tableView and make it proper scrollable.

Here is my code to set the content size of scrollView.

float maxHeight = 0;

    for(UIView *v in [self.scrollView subviews]){
        if(v.frame.origin.x + v.frame.size.height > maxHeight)
            maxHeight = v.frame.origin.x + v.frame.size.height;
    }

    self.scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width, maxHeight+100);

Upvotes: 0

Views: 1916

Answers (3)

David Hoerl
David Hoerl

Reputation: 41622

A tableview is a scroll view subclass, and generally Apple says adding it to another scroll view will cause problems. That said there are ways to make it work. Search here or on google for terms like "how to put a uitableview in a uiscrollview".

EDIT: to make it not scrollable you could put a transparent view over the table view that stops or eats touch events.

Upvotes: 2

alex-i
alex-i

Reputation: 5454

First you'll need to resize your table, so that it fits all the rows (since as you said, it's not scrollable). I believe that doing so will make all your rows to not be reusable, so if it's a big table you may get performance issues. Also be aware of @David H's answer, so if you really want to do this, you might be better of using another UIScrollView instead of the UITableView (it won't bring much benefit either way).

Second, you'll need to move all the views beneath the tableview, so that they won't overlap. An easy way to do this is to have those views put together in a different view, so that you only need to move one view instead (in this case however, you'll also need to resize that view accordingly).

Finally, instead of looping through all the scrollview's subviews, it should be easy enough to keep track of the view that is closest to the bottom (this depends on how you generate and add the views to the scroll view).

Upvotes: 2

Desert Rose
Desert Rose

Reputation: 3424

self.scrollingView.contentSize=CGSizeMake(320,372);

Try this.

Upvotes: 0

Related Questions