UIViewController没有填充屏幕

时间:2011-02-08 22:05:43

标签: iphone uiview uiviewcontroller uinavigationcontroller uinavigationbar

我有一个推动另一个UIViewController的UINavigationController。在这个UIViewController中,我将在纵向模式下显示UITableView,在横向模式下显示另一个视图。

因此,在我的viewDidLoad中,我正在创建UIView,然后向此添加2个ViewControllers。我的问题是,当它加载时,我在顶部得到以下白色边距。 example

我认为这是因为(在我的下面的第3步中)

CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
[[self view] setFrame:appFrame];

没有返回全屏,减去导航栏。这是正确的吗?如果是这样我怎么能让它返回完整的尺寸,所以没有白边?

- (void)viewDidLoad 
{
    [super viewDidLoad];

    // Step 1 - Set up the tableDataView for vertical layout
TableDataViewController *tableController = [[TableDataViewController alloc] init];
self.tableDataViewController = tableController;
[tableController release];

// Step 2 - Set up the graphView for horizontal layout
GraphViewController *graphController = [[GraphViewController alloc] 
    initWithNibName:@"GraphViewController" bundle:nil];
self.graphViewController = graphController;
[graphController release];

// Step 3 - Get the screen size
CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
[[self view] setFrame:appFrame];

// Step 4 - Add the vertical view to the containerView
//        and then add the containerView to this controller
containerView = [[[UIView alloc] initWithFrame:appFrame] autorelease]; 
[[self view] addSubview:containerView];                     
[containerView addSubview:[tableDataViewController view]];

// Step 5 - Add to the active view so we can test for it later
activeView = [tableDataViewController view];    
 }

非常感谢

麦克

2 个答案:

答案 0 :(得分:2)

我认为您的帧偏移存在问题。启用导航栏后,您在appFrame中获得的矩形偏移量为44.f(导航栏的高度) - 请检查NSLog并查看是否为真。

因为您要设置将放置在原点的视图的框架,所以应将x和y原点设置为零。您可以通过检查

以更安全的方式执行此操作
CGFloat navHeight = navController.navigationBarHidden ? 0 :
    navController.navigationBar.frame.size.height;

实际上我认为使用[UIScreen mainScreen]的bounds属性可能是一个更好的整体解决方案。它将正确设置原点和大小,您不需要检查导航栏的存在。

检查发生了什么:

CGRect screenBounds = [[UIScreen mainScreen] bounds]
NSLog(@"%@", NSStringFromCGRect(screenBounds));
CGRect screenFrame = [[UIScreen mainScreen] applicationFrame]
NSLog(@"%@", NSStringFromCGRect(screenFrame));

答案 1 :(得分:0)

有几件事正在发生。最重要的是,frame在其超级视图的坐标系中描述了视图的位置

containerView似乎被添加为自定义视图的子视图,其坐标系起源于导航栏,而不是应用程序框架。

您想要填充自定义视图,因此containerView的框架应该类似于

CGRectMake(0.0, 0.0, self.view.bounds.width, self.view.bounds.height);