在CGRectZero中使用autoResizingMask

我正在build立一个tableview部分的页脚。 页脚的高度将在heightForFooterInSection指定,所以在viewForFooterInSection我想只是添加子视图,并指定页脚视图应填充任何页脚高度指定(此页脚大小将dynamic)。 因此,我使用CGRectZero作为初始框架并告诉页脚视图展开以填充其父视图。

 - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { UIView *footerView = [[UIView alloc] initWithFrame:CGRectZero]; footerView = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; footerView = [UIColor greenColor]; return footerView; } 

这按预期工作 – 表视图的页脚完全填充绿色视图。

但是现在我想添加一个UITextView到页脚。 文本视图应填充相同的空间,但留下5点边界:

 { UIView *footerView = [[UIView alloc] initWithFrame:CGRectZero]; footerView = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; footerView = [UIColor greenColor]; UITextView *textView = [[UITextView alloc] initWithFrame:CGRectInset(footerView.frame, 5, 5)]; textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; textView.backgroundColor = [UIColor redColor]; [footerView addSubview:textView]; return footerView; } 

而不是填充页脚视图(5点余量),文本视图根本不出现。 它可能有一个CGRectZero框架(或甚至-5 x -5?)。 如果我将插入设置为0,0,但是,它按预期展开。

这是什么解释? 如果我不能在初始帧中使用CGRectZero的插图,那么当footerView的框架不能被知道的时候,我期望使用什么?

CGRectInset将根据现有的矩形创build一个矩形。 它不会再一次引用页脚视图:只有当它正在计算它这一次。 在这种情况下,由于您试图插入一个零大小的矩形,这适用于文档:

讨论 。 矩形是标准化的,然后插入参数被应用。 如果生成的矩形将具有负高度或宽度,则返回空矩形。

因此,您正在创build一个空矩形的标签。

我会创build一个“典型”大小的页脚,然后用您想要的autoResizingMask大小适当的标签, 然后将您的footerView设置为零,如果这是你想要的设置。

我猜猜TextView会在后台创build内容,所以在初始化时它是空的。 我通常最终使用[string sizeWithFont:constrainedToSize:lineBreakMode:]

 CGSize size = [aString sizeWithFont:[UIFont systemFontOfSize:12.0] constrainedToSize:CGSizeMake(320,500) lineBreakMode:NSLineBreakByWordWrapping]; CGRect frame = CGRectMake(0, 0, size.width, size.height);