2013-04-29 58 views
0

我想添加一个视图到集合视图和表视图(因此适用于任何类型的滚动视图)的内容视图的底部,我也希望能够向下滚动以查看此视图​​,例如:UICollectionView等计算内容大小,以便我可以永久增加它?

- (id)initWithCoder:(NSCoder *)aDecoder 
{ 
    self = [super initWithCoder:aDecoder]; 
    if (self) { 
     // Observe change in content size so can move my view when 
     // content size changes (keep it at the bottom) 
     [self addObserver:self forKeyPath:@"contentSize" 
        options:(NSKeyValueObservingOptionPrior) 
        context:nil]; 
     CGRect frame = CGRectMake(0, 0, 200, 30); 
     self.loadingView = [[UIView alloc] initWithFrame:frame]; 
     [self.loadingView setBackgroundColor:[UIColor blackColor]]; 
     [self addSubview:self.loadingView]; 
     // Increase height of content view so that can scroll to my view. 
     self.contentSize = CGSizeMake(self.contentSize.width, self.contentSize.height+30); 

    } 
    return self; 
} 

然而,当,例如,被插入细胞中的contentSize被重新计算并同时我的观点是在内容大小的底部仍然可见(由于是超生滚动视图)我不能再滚动到它。

如何确保内容大小保持不变,正如在我的代码中那样,高30点?

另外一个问题是:

  • 有没有其他的方法来跟踪观察比它的其他内容的大小?

在此先感谢。


我曾尝试:

- (void)layoutSubviews 
{ 
    [super layoutSubviews]; 
    self.contentSize = CGSizeMake(self.contentSize.width, self.contentSize.height+30); 
} 

然而这会导致各种各样的显示问题。

+0

你可能想在layoutSubviews做这些计算。 – atreat 2013-04-29 18:59:25

+0

这是UICollectionView等通常计算contentSize的地方吗? – Michael 2013-04-29 19:00:23

+0

我没有看到代码中的任何内容会改变您正确的“contentSize' – matt 2013-04-29 19:05:28

回答

0

如果我理解正确,你想要在底部的tableView(f.e.)中显示一个加载视图。你可以添加一个额外的包含这个LoaderView的UITableViewCell到tableView。 (必须更改numberOfRowsInTableView)

对于scrollViews的另一个角度:使用较小的边界,然后使用内容本身,以使其可滚动。例如frame =全屏。在子视图中添加或修改每个单元格(添加)contentSize =内容大小+ 30 px。

+0

是的,这是我目前所拥有的,但是我的应用程序中有5个实例, UITableView和UICollectionViews加载视图。我试图通过将其构建到UIxxxView本身中以某种方式进行抽象。 – Michael 2013-04-29 19:11:22

+0

可能通过永久增加单元格数量并设置最后一个单元格的大小来在ViewController中对其进行抽象,但是我喜欢使用委托协议在触发“触发器行”时向接收器发送消息的想法,然后委托插入更多数据。 – Michael 2013-04-29 19:14:48

+0

在这种情况下:如何将类LoaderView作为属性添加到UIScrollView类以及覆盖的setContentSize方法,该方法使用loaderView的高度自动增加contentSize.y。你可以在这里找到更多有关房产的信息:http:// oleb。净/博客/ 2011/05 /伪造,高德式-objc类别与 - 联想引用/ – Peteee24 2013-04-29 19:24:38

0

尝试制作滚动视图的子类并重写contentSize getter,以便始终返回30 px。

- (CGSize)contentSize { 
    CGSize customContentSize = super.contentSize; 
    customContentSize.height += 30; 
    return customContentSize; 
} 

(我写的内存代码,可能有误差)

相关问题