缩放UIScrollView contentSize以适合内部可变高度UITableView的UIView [英] scaling UIScrollView contentSize to fit UIView with variable-height UITableView inside

查看:39
本文介绍了缩放UIScrollView contentSize以适合内部可变高度UITableView的UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个uiscrollview的uiview,它是笔尖的子视图.然后,我将另一个uiview和uitableview加载为该另一个uiview的子视图(此视图内的其他元素)作为uiscrollview的子视图.

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.

即,UISCrollView-> UIView-> UITableView(以及UIView内部的其他一些视图).

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

我需要执行以下操作:

一旦我知道了tableview的最终高度(取决于单元格的数量和它们的高度),我需要调整包含它的uiview的大小,并相应地调整包含scrollview的contentSize属性的大小.

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.

但是我不知道表格视图的最终大小在哪一点(因为它可以根据其单元格所容纳的文本量来动态更改大小),我也不知道表格视图将超出多少uiview(默认为320 x 460).

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).

我尝试在viewdidload上将包含tableview的视图设置为900的高度,并将其设置为sizeToFit,希望当将tablecell添加到tableview时它会缩小(假设在添加cell的情况下tableview的框架会适当缩放).但这似乎不起作用.

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.

有什么想法吗?谢谢!

推荐答案

我只是遇到了类似情况(UIScrollView-> UITableView),并用了好几个小时不停地动脑筋.简短的答案是:在向我的ViewController(控制UIScrollView)发送 viewWillAppear:之前,没有可用的正确尺寸作为尺寸调整的基础.在此之前,我发现这些值已设置,但通常是错误的.(例如,我第一次尝试这样做是在 viewDidLoad:中,并且大小值更改了,但是它们是错误!)

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:

  • viewDidLoad:中,一旦放置好数据,就会使随附的UITableView变为 reloadData .
  • viewWillAppear中的
  • :执行以下操作(假定 tableView 是附带的UITableView,而 scrollView 是附带的UIScrollView):

  • 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.,当我在 viewWillAppear:之前检查它们时,我得到了一些非常奇怪的数字;例如,tableView.contentSize似乎是默认rowHeight *行数的乘积,忽略了我的自定义行高并排除了任何分隔符!很奇怪.

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.

这篇关于缩放UIScrollView contentSize以适合内部可变高度UITableView的UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆