在CGRectZero中使用autoResizingMask [英] Using autoResizingMask with CGRectZero
问题描述
我正在为tableview的部分建立页脚.页脚的高度将在heightForFooterInSection
中指定,因此在viewForFooterInSection
中,我只想添加子视图并指定页脚视图应填充指定的任何页脚高度(此页脚大小将是动态的).因此,我将CGRectZero
用作初始框架,并告诉页脚视图展开以填充其父视图.
I am building a footer for a tableview's section. The height of the footer will be specified in heightForFooterInSection
, so in viewForFooterInSection
I would like to just add the subview and specify that the footer view should fill whatever footer height is specified (this footer size will be dynamic). So, I am using CGRectZero
as the initial frame and telling the footer view to expand to fill its parent view.
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *footerView = [[UIView alloc] initWithFrame:CGRectZero];
footerView = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
footerView = [UIColor greenColor];
return footerView;
}
这可以按预期工作-表格视图的页脚完全用绿色视图填充.
This works as expected - the footer of the table view is filled completely with the green view.
但是现在我想在页脚中添加UITextView
.文本视图应填充相同的空间,但保留5点边框:
But now I want to add a UITextView
to the footer. The text view should fill the same space, but leave a 5-point border:
{
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,则它会按预期扩展.
Instead of filling the footer view (with a 5 point margin), the text view does not appear at all. It likely has a frame of CGRectZero
(or maybe even -5 x -5?). If I set the inset to 0, 0, however, it expands as expected.
对此有何解释?并且如果我不能在初始帧中使用CGRectZero
的插图,那么当footerView的帧未知时,我应该使用什么?
What is the explanation for this? And If I can't use an inset of CGRectZero
for the initial frame, what am I expected to use when the frame of the footerView can not be known?
推荐答案
CGRectInset将基于现有矩形创建一个矩形.它不再引用页脚视图:仅当它正在计算一次时.在这种情况下,由于您要插入大小为零的矩形,因此适用于文档:
CGRectInset will create a rectangle based on the existing rectangle. It doesn't refer to the footer view ever again: only when it is calculating it this one time. In this case, since you are trying to inset a rectangle with a zero size, this applies from the docs:
讨论. 矩形已标准化,然后插入参数为 应用.如果所得矩形的高度为负,或 宽度,则返回一个空矩形.
Discussion. The rectangle is standardized and then the inset parameters are applied. If the resulting rectangle would have a negative height or width, a null rectangle is returned.
因此,您正在使用一个空矩形创建标签.
Therefore, you are creating your label with a null rectangle.
我将创建典型"尺寸的页脚,然后使用所需的autoResizingMask创建适当尺寸的标签,然后然后将footerView设置为零(如果您希望将其设置为零)
I would create the footer with a "typical" size, then an appropriately sized label with the autoResizingMask that you want, and then set your footerView to zero if that is what you want it set to.
这篇关于在CGRectZero中使用autoResizingMask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!