简体   繁体   English

Objective C-设置子视图框架的正确方法,以使其充满屏幕

[英]Objective c - proper way to set subview frame so it will fill the screen

I have a viewController inside of a navigationController , the view controller has a tableview. 我有一个内部的的viewController navigationController ,视图控制器具有的tableview。
In viewDidLoad I set the tableview viewDidLoad我设置了tableview

- (void)viewDidLoad
{
    [super viewDidLoad];

    // init tableView
    CGRect tableFrame = self.view.bounds;

    _tableView = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStylePlain];

    _tableView.delegate = self;
    _tableView.dataSource = self;

    [self.view addSubview:_tableView];
}

The problem with this code is that the table view frame is not correct - the height is 460 and I need it to be 416. 此代码的问题是表格视图框架不正确-高度为460,我需要为416。

[The iPhone screen height is 480, minus the status bar (20) minus the navigation bar (44) = 416] [iPhone屏幕高度为480,减去状态栏(20)减去导航栏(44)= 416]

So what is the proper way to set the table view so it will fill the screen? 那么设置表格视图以使其充满屏幕的正确方法是什么?
I can think of two ways: 我可以想到两种方式:

  1. set its frame to = (0, 0, 320, 416) 将其框架设置为= (0, 0, 320, 416)

  2. use: [_tableView setAutoresizingMask:(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)]; 使用: [_tableView setAutoresizingMask:(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)];

Don't use magic numbers. 不要使用幻数。 Use the resizing flags correctly. 正确使用大小调整标志。 So yes, your 2. approach is correct. 是的,您的方法是正确的。

1) Use the superviews bounds. 1)使用超级视图范围。
_tableView.frame = self.view.bounds; ; ;

2) Set autoresizing flags 2)设置自动调整大小标志
[_tableView setAutoresizingMask: UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];

(you did all of this already :) (您已经完成了所有这些操作:)

Number 1 is absolutely the wrong way to do it... what happens if the screen size changes in a future OS / device? 数字1绝对是错误的方法...如果将来的OS /设备上的屏幕尺寸发生变化,会发生什么?

I'm curious why you're not doing this using a nib file, and saving yourself the trouble, but if you must do it in code, set the auto-resizing mask per your option 2. 我很好奇为什么不使用nib文件来这样做,省却了麻烦,但是如果您必须在代码中执行此操作,请根据选项2设置自动调整大小掩码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM