1

我有一个UINavigationController,推上另一个UIViewController。在这个UIViewController中,我将在纵向模式下显示一个UITableView,在横向模式下显示另一个视图。UIViewController没有填充屏幕

因此,在我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

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

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

CGFloat navHeight = navController.navigationBarHidden ? 0 : 
    navController.navigationBar.frame.size.height; 

事实上,我认为使用[UIScreen mainScreen]的边界属性可能是一个更好的整体解决方案以更安全的方式做到这一点。它会随着原点和尺寸的设置而正确显示,您不需要检查导航栏的存在。

检查是怎么回事:

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

当我你有什么建议上面我得到以下几点:Screenbounds {{0,0},{ 320,480}},Screenframe {{0,20},{320,460}}。所以理想情况下,我需要使用[[UIScreen mainScreen]边界]来获得最大尺寸。但是,我这样做,导航栏下方仍有一个白色边距。这就像是完全无视它。麦克风。 – hydev 2011-02-10 21:53:44

0

有两件事情正在进行。首先,frame在其超级视图的坐标系中描述视图的位置

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

要填充的自定义视图,这样的containerView框架应该像

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