为什么我的UIVIew会在屏幕外加载?

时间:2011-01-18 15:32:07

标签: iphone uiview

当我的应用程序启动时,我执行以下代码:

LViewController *aLViewController = [[LViewController alloc] initWithNibName:@"LViewController" bundle:[NSBundle mainBundle]];
self.viewController = aLViewController;
[aLViewController release];  
[window addSubview:[viewController view]];

但是视图总是加载得太高而且隐藏在状态栏后面。我已经尝试在Interface Builder中更改模拟的用户元素以包含状态栏,但它没有帮助。

1 个答案:

答案 0 :(得分:2)

将视图添加到窗口时,您必须考虑状态栏的高度:

CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
CGSize statusBarSize = statusBarFrame.size;
CGFloat statusBarHeight = statusBarSize.height;

LViewController *aLViewController = [[LViewController alloc] initWithNibName:@"LViewController" bundle:[NSBundle mainBundle]];
self.viewController = aLViewController;
[aLViewController release];  

viewController.view.frame = CGRectMake(0.0f,
                                       statusBarHeight,
                                       window.bounds.size.width,
                                       window.bounds.size.height - statusBarHeight);

[window addSubview:[viewController view]];