2012-04-26 22 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

相關問題