简体   繁体   English

如何将子视图大小调整传播到superview大小调整?

[英]How do I propagate subview resizing to superview resizing?

I'm trying to do the opposite of what the autoresizing mask automatically does. 我试图与自动调整掩码自动执行相反的操作。 That is, I have a view hierarchy and I need to propagate a change in the deepest subview frame all the way up the chain, so that all the subviews resize to fit the newer (possibly bigger) subviews. 也就是说,我有一个视图层次结构,我需要在最深的子视图框架中向上传播一个更改,以便所有子视图调整大小以适应更新的(可能更大的)子视图。

Specifically, I have a scroll view that has aa subview as a container, which contains a table, and another view which in turn contains an image and another table. 具体来说,我有一个滚动视图,它有一个子视图作为容器,其中包含一个表,另一个视图又包含一个图像和另一个表。 Like so: 像这样:

在此输入图像描述

And this is the chain of events: 这是一系列事件:

A) 一种)

1 The innermost table, of 2 rows, might have to stretch down if the name of the person is too long (meaning the cell heights are variable and calculated at runtime on heightForCellAtIndexPath). 1如果人的名字太长(表示单元格高度是可变的并且在运行时在heightForCellAtIndexPath上计算),则最内层的表(2行)可能必须向下拉伸。

2 If so, this needs to trigger the stretching of the container view for this table and the image, so that no cells of the table end up overflowing the view. 2如果是这样,则需要触发此表和图像的容器视图的拉伸,以使表中的单元格不会溢出视图。

3 And if that's the case, then the other table below this container view needs to translate down a bit to make sure it's not overlapping. 3如果是这种情况,那么此容器视图下方的另一个表需要向下转换一点以确保它不重叠。 Note that the chain could also start here, even if steps 1,2 don't take place. 请注意,即使步骤1,2没有发生,链也可以从这里开始。 This would be the case if any this table's 3 rows happens to stretch to accommodate longer text. 如果任何此表的3行恰好拉伸以容纳更长的文本,则会出现这种情况。

4 If any of the above result in an increase in length, the outermost container view needs to stretch in length too. 4如果上述任何一个导致长度增加,则最外面的容器视图也需要拉伸长度。

5 Finally, if this outer container view did stretch, the root scrollview needs to change its contentSize property so that the new bigger view fits and can be fully scrolled if bigger than the device's screen. 5最后,如果此外部容器视图确实拉伸,则根卷轴视图需要更改其contentSize属性,以便新的更大视图适合并且如果大于设备的屏幕则可以完全滚动。

The first problem is knowing when (at what point in code) is the innermost table done laying out its cells and its final frame computed. 第一个问题是知道什么时候(在代码中的什么位置)最内层的表完成布局其单元格并计算其最终帧。 If I knew when is the final frame known, I could send a KVO notification on view.frame change to the single view controller managing all these views. 如果我知道最终帧何时知道,我可以将view.frame更改的KVO通知发送到管理所有这些视图的单个视图控制器。 But then the controller would have to manually resize all the subviews frames. 但是控制器必须手动调整所有子视图帧的大小。 I've tried this approach but the frames don't seem accurate. 我尝试过这种方法,但框架似乎不准确。 I'm wondering if there might be timing issues I'm not considering. 我想知道是否可能存在我不考虑的时间问题。

This works for instance: 这适用于例如:

- (void)viewDidLoad
{
  ...

  [self addObserver:self forKeyPath:@"attendeeContentView.frame" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:NULL];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 
{
  if([keyPath isEqualToString:@"attendeeContentView.frame"]) 
  {
    CGRect newFrame = [[change objectForKey:NSKeyValueChangeOldKey] CGRectValue];
    UIScrollView *scrollView = (UIScrollView*)self.view;
    scrollView.contentSize = CGSizeMake([[UIScreen mainScreen] applicationFrame].size.width, newFrame.size.height + newFrame.origin.y);
    NSLog(@"\n new contentSize: %f", newFrame.size.height + newFrame.origin.y);
  }
}

But this is only the final link in the chain. 但这只是链中的最后一环。 It all starts with the innermost table expanding to fit all cells... 这一切都始于最里面的桌子扩展以适应所有细胞......

Any ideas? 有任何想法吗?

You can just check for the last call of - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 您可以检查最后一次调用- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

if (indexPath.row == (NumberOfCells - 1)) {
        // update your views (preferably call it after a slight delay)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM