ios - 如何理解 table.autoresizingMask 的行为?

标签 ios uitableview uiviewcontroller

我了解到在单 View 应用程序中设置覆盖整个主视图(全宽和全高)的 UITableView 的正确方法是,在 viewDidLoad:

table = [[UITableView alloc] initWithFrame:self.view.bounds];
table.autoresizingMask = UIViewAutoresizingFlexibleWidth | 
                             UIViewAutoresizingFlexibleHeight;
[self.view addSubview:table];  

请注意,如果物理 iPad 2 处于横向模式,并且上面的 self.view.bounds 打印在 viewDidLoad 中,它将显示:{{0, 0}, {768, 1004}}。所以我的想法是:不要担心宽度和高度不正确,因为 UIViewAutoresizingFlexibleWidthUIViewAutoresizingFlexibleHeight 会自动设置正确的宽度和大小。

所以我实际上尝试将上面的第一行替换为:

table = [[UITableView alloc] init];

table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];

甚至

table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 1004, 768)];

或最终的“正确值”:

table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 1024, 748)];

当 iPad 2 处于横向位置时。但是现在表格不会完全扩展到整个主视图。那么这里的管理原则是什么?宽高可以设置错,但一定是错在{768, 1004}?它不能与其他值“不正确”吗?如果没有给出 CGRect,或者给出了一些伪值 {200, 200}viewDidLoad 中的代码应该做什么来制作表格是否具有完整的主视图的宽度和高度,无论是横向模式还是纵向模式?

最佳答案

不要使用bounds,而是使用frame:

table = [[UITableView alloc] initWithFrame:self.view.frame];
table.autoresizingMask = UIViewAutoresizingFlexibleWidth | 
                         UIViewAutoresizingFlexibleHeight;

关于ios - 如何理解 table.autoresizingMask 的行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12207633/

相关文章:

ios - 如何将 tableview 单元格附件应用于 tableview 记录

ios - 什么会导致 UITableView 获得清晰的背景?

iphone - View 出现在键盘窗口上。如何解决

ios - scrollView 边界大小始终为零

ios - 获取对当前可见 View Controller 的引用

ios - 如果电子邮件未经验证,Apple将替换电子邮件提供商

ios - XML 解析器正在阻塞主线程

iphone - 获得在 iPhone 中播放的音乐的力量

ios - 执行选择器可能会因为select未知而导致泄漏?

iPhone 开发,UITableView 背景颜色