ios - 将 autoResizingMask 与 CGRectZero 一起使用

标签 ios cocoa-touch uitableview uiview autoresizingmask

我正在为表格 View 的部分构建页脚。页脚的高度将在 heightForFooterInSection 中指定,因此在 viewForFooterInSection 中,我只想添加 subview 并指定页脚 View 应填充指定的任何页脚高度(此页脚大小将是动态的)。因此,我使用 CGRectZero 作为初始框架并告诉页脚 View 展开以填充其父 View 。

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

这按预期工作 - 表格 View 的页脚完全被绿色 View 填充。

但现在我想在页脚中添加一个 UITextView。 TextView 应填充相同的空间,但保留 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;
}

TextView 根本不显示,而不是填充页脚 View (具有 5 点边距)。它可能有一个 CGRectZero 的框架(或者甚至可能是 -5 x -5?)。但是,如果我将 inset 设置为 0, 0,它会按预期扩展。

请问这是怎么解释的?如果我不能为初始框架使用 CGRectZero 的插图,那么当无法知道 footerView 的框架时,我应该使用什么?

最佳答案

CGRectInset 将基于现有矩形创建一个矩形。它不再引用页脚 View :仅当它计算一次时。在这种情况下,由于您试图插入一个大小为零的矩形,因此这适用于文档:

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.

因此,您正在创建带有空矩形的标签。

我会创建一个“典型”尺寸的页脚,然后是一个带有您想要的 autoResizingMask 的适当大小的标签,然后然后将您的 footerView 设置为零(如果您希望将其设置为零) .

关于ios - 将 autoResizingMask 与 CGRectZero 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13905144/

相关文章:

iphone - ASIHTTPRequest 假人

ios - 在 obj-c 中使用参数初始化

ios - 如何在单个表中使用两个不同的自定义 UITableViewCell 子类?

ios - UITableView 数据源必须从 tableView :cellForRowAtIndexPath 返回单元格

ios - 图像和 scrollView 渲染使我的 tableview 不稳定

ios - UITableView 中的后退按钮

ios - 当设备在 iOS 7 上进入休眠状态时,clock_get_time/mach_absolute_time 停止更新

ios - 目前哪些设备在预览模式下支持 Apple ARKit?

objective-c - NSDateFormatter 不格式化(返回 nil)

ios - UITableViewCell 覆盖 : when alloc UILabel in cellForRowAtIndexPath?