2012-04-26 51 views
3

我有UINavigationBar的视图: 的AppDelegate通过initWithFrame添加子视图。 UINavigationBar的大小

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
// Override point for customization after application launch. 

_homeScreen = [[PrepareScreen alloc] init]; 
self.mainViewController = [[MyViewController alloc] initWithScreenDef:[self.homeScreen projectHomeScreen] AndBounds:self.window.bounds]; 

self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.mainViewController]; 
self.window.rootViewController = self.navigationController; 
[self.window makeKeyAndVisible]; 
return YES; 

后来才知​​道有一类

MyViewController : UIView <UITableViewDataSource,UITableViewDelegate> 
的.h文件

,我有:

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

self.myTableView = [[UITableView alloc] initWithFrame:self.view.bounds]; 
[self.view addSubview:self.myTableView]; 
} 

之后,我有一个错误的他屏幕的ight。我希望我的myTableView有一个size.height-49.0

回答

2

初始化了的tableView与CGRectZeroviewDidLoad方法,然后在方法viewWillAppear:设置self.myTableView.frame在这种方法中,视图的几何体已经被设置和self.view.bounds将是正确的。所以一般来说:在viewDidLoad中的初始化视图,但在viewWillAppear:中设置它们的几何图形。

+0

卓越的解决方案。谢谢! – Kuba 2012-04-26 14:57:12

+1

只有一个补充,你可以在viewWillAppear:中创建你的表格视图,只要你不做大量的操作。通常使用viewWillAppear来进行轻量级操作 - 主要是几何图形调整。 – graver 2012-04-26 14:59:36

+0

好的,所以当我想从我的模型类中放入数据是更好的做到这一点viewDidLoad,当我试图调整大小单元格或更改背景颜色(例如)我应该使用viewWillAppear,对不对? – Kuba 2012-04-26 15:02:06

0

这是怎么回事?

CGRect r = initWithFrame:self.view.bounds; 
r.size.height -= 49.0; 
self.myTableView = [[UITableView alloc] initWithFrame:r]; 

希望它可以帮助...

+0

是的,这是显而易见的解决方案,但我希望更具普遍性。也许获得实际的可用空间大小。 – Kuba 2012-04-26 14:54:29