开发者

scaling UIScrollView contentSize to fit UIView with variable-height UITableView inside

开发者 https://www.devze.com 2023-04-06 09:04 出处:网络
I have a uiview with a uiscrollview as a subview in a nib. Then, I load another uiview with a uitableview as a subview of this other uiview (among other elements inside this view) as a subview of the

I have a uiview with a uiscrollview as a subview in a nib. Then, I load another uiview with a uitableview as a subview of this other uiview (among other elements inside this view) as a subview of the uiscrollview.

That is, UISCrollView -> UIView -> UITableView (and some other views inside the UIView).

And I need to do the following:

Once I know the final height of the tableview (depending on the number of cells and height of them) I need to resize the uiview containing it and, in turn, resize the contentSize property of the containing scrollview.

But I don't know at which point the final size of the tableview is known (because it can dynamically change size depending on the amount of text its cells will hold) and neither do I know by how much will the tableview exceed the uiview (which by default is 320 x 460).

I've tried setting the view containing the tableview to a height of 900 on viewdidload and开发者_开发技巧 setting it to sizeToFit, hoping it would shrink as tablecells are added to the tableview (assuming as cells are added the tableview's frame would scale appropriately). But this doesn't seem to work.

Any thoughts? Thanks!


I just ran into a similar situation (UIScrollView -> UITableView) and was banging my head against it for hours. The short answer is this: the correct sizes to use as the basis of sizing things out were not available until my ViewController (governing the UIScrollView) was sent viewWillAppear:. Before then, I found that the values were set, but often wrong. (E.g., my first attempt at doing this was in viewDidLoad:, and the sizing values were changed there, but they were wrong!)

So my solution was thus:

  • In viewDidLoad: cause your enclosed UITableView to reloadData once you have your data in place.
  • in viewWillAppear: do the following (presuming tableView is the enclosed UITableView and scrollView is the enclosing UIScrollView):

    tableView.frame = (CGRect){ 
        tableView.frame.origin,
        tableView.contentSize 
    };
    // Finally, set content size based on the last element in the scrollView
    // in my case, the last element IS the tableView, yours might be different
    scrollView.contentSize = 
        CGSizeMake(scrollView.width 
                   - scrollView.contentInset.left
                   - self.scrollView.contentInset.right,
                   tableView.top
                   + tableView.height);
    

N.B., I was getting some very weird numbers when I checked them before viewWillAppear:; e.g., the tableView.contentSize seemed to be the product of default rowHeight * number of rows, ignoring my custom row heights and excluding any separators! Very odd.


This is just a guess, but since UITableView is a subclass of UIScrollView, can you use the contentSize property to see its overall size after a call to reloadData? You could use the size of the tableview to resize the UIView and the contentSize of the scrollView.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号