UINavigationController 中的导航栏位置不正确

发布于 2024-09-02 19:57:49 字数 2332 浏览 1 评论 0原文

因此,我的 iPad 程序有一个伪分割视图控制器(我实现的一个,而不是基础 SDK 的控制器),并且不久前工作正常。它具有基本布局(UINavController 用于主视图,内容视图控制器用于右侧的详细信息),但我拥有它,因此主视图在旋转到纵向视图时不会消失。

最近,我添加了一个 UITabBarController 来包含整个分割视图,这使得导航栏变得不稳定,而所有其他视图都定位得很好。此外,只有当 iPad 处于横向或颠倒纵向状态时程序启动时,导航栏才会错位。如果从肖像开始,一切都很好。

示例图像可以在这里找到: http://profile.imageshack.us/user/Pzychotix

导航栏向上的图像是我最初启动该程序的时候。 导航栏向下的图像是我旋转一次或多次后的图像。

相关代码:

RootViewController.m:
- (void)loadView {
navController = [[NavigationBreadcrumbsController_Pad alloc] init];

ABTableViewController_Pad * tableViewController = [[ABTableViewController_Pad alloc] initWithNibName:@"ABTableView"];

master = [[UINavigationController_Pad alloc] initWithRootViewController:tableViewController];
[tableViewController release];

// Dummy blank UIViewcontroller
detail = [[UIViewController alloc] init];
detail.view = [[[UIView alloc] init] autorelease];
[detail.view setBackgroundColor:[UIColor grayColor]];

self.view = [[[UIView alloc] init] autorelease];
self.view.backgroundColor = [UIColor blackColor];
[self positionViews];
[self.view addSubview:navToolbarController.view];
[self.view addSubview:master.view];
[self.view addSubview:detail.view];
}


// Handles the respositioning of view into it's current orientation
-(void)positionViews{

CGFloat tabBarOffset = 0;

if(self.tabBarController){
    tabBarOffset = self.tabBarController.tabBar.frame.size.height;
}

if(self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
    self.view.frame = CGRectMake(0, 0, 768, 1004);
    navController.view.frame = CGRectMake(0,0,768,44);
    //adjust master view
    [master.view setFrame:CGRectMake(0, 44, 320, 1024 - 44 - 20 - tabBarOffset)];

    //adjust detail view
    [detail.view setFrame:CGRectMake(321,44, 448, 1024 - 44 - 20 - tabBarOffset)];
}
// Landscape Layout
else{
    self.view.frame = CGRectMake(0, 0, 748, 1024);
    navToolbarController.view.frame = CGRectMake(0,0,1024,44);
    //adjust master view
    [master.view setFrame:CGRectMake(0, 44, 320, 768 - 44 - 20 - tabBarOffset)];

    //adjust detail view
    [detail.view setFrame:CGRectMake(321,44, 1024 - 320, 768 - 44 - 20 - tabBarOffset)];
}

}

So, my iPad program has a pseudo-split view controller (one that I implemented, not base SDK one), and was working correctly a while ago. It has the basic layout (UINavController for master, content view controller for detail on right), but I have it so the master view doesn't disappear when rotated into portrait view.

Recently, I added in a UITabBarController to contain the entire split view, which has made the navigation bar go wonky, while all the other views are positioned fine. In addition, the navigation bar only gets mispositioned when the program starts up while the iPad is in landscape, or upside-down portrait. If it starts out in portrait, everything is fine.

Example images can be found here:
http://profile.imageshack.us/user/Pzychotix

Image where the navigation bar is upwards is when I initially launch the program.
Image where the navigation bar is downwards is after I rotate once or more times.

Relevant Code:

RootViewController.m:
- (void)loadView {
navController = [[NavigationBreadcrumbsController_Pad alloc] init];

ABTableViewController_Pad * tableViewController = [[ABTableViewController_Pad alloc] initWithNibName:@"ABTableView"];

master = [[UINavigationController_Pad alloc] initWithRootViewController:tableViewController];
[tableViewController release];

// Dummy blank UIViewcontroller
detail = [[UIViewController alloc] init];
detail.view = [[[UIView alloc] init] autorelease];
[detail.view setBackgroundColor:[UIColor grayColor]];

self.view = [[[UIView alloc] init] autorelease];
self.view.backgroundColor = [UIColor blackColor];
[self positionViews];
[self.view addSubview:navToolbarController.view];
[self.view addSubview:master.view];
[self.view addSubview:detail.view];
}


// Handles the respositioning of view into it's current orientation
-(void)positionViews{

CGFloat tabBarOffset = 0;

if(self.tabBarController){
    tabBarOffset = self.tabBarController.tabBar.frame.size.height;
}

if(self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
    self.view.frame = CGRectMake(0, 0, 768, 1004);
    navController.view.frame = CGRectMake(0,0,768,44);
    //adjust master view
    [master.view setFrame:CGRectMake(0, 44, 320, 1024 - 44 - 20 - tabBarOffset)];

    //adjust detail view
    [detail.view setFrame:CGRectMake(321,44, 448, 1024 - 44 - 20 - tabBarOffset)];
}
// Landscape Layout
else{
    self.view.frame = CGRectMake(0, 0, 748, 1024);
    navToolbarController.view.frame = CGRectMake(0,0,1024,44);
    //adjust master view
    [master.view setFrame:CGRectMake(0, 44, 320, 768 - 44 - 20 - tabBarOffset)];

    //adjust detail view
    [detail.view setFrame:CGRectMake(321,44, 1024 - 320, 768 - 44 - 20 - tabBarOffset)];
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅笑依然 2024-09-09 19:57:49

好吧,我已经找到了一个解决方案,尽管我仍然摸不着头脑,不知道它为什么有效。

基本上,我在 UINavigationController 上调用了layoutIfNeeded,这解决了所有问题。我不明白的是为什么它之前可以工作,或者为什么我需要调用layoutIfNeeded,因为我假设 setFrame 会自动处理布局控制器的任何子视图。

Well I've found a solution, though I'm still scratching my head as to why it's working.

Basically, I called layoutIfNeeded on my UINavigationController, and that fixed everything right up. What I don't understand is why it was working before, or why I would need to call layoutIfNeeded, as I assumed setFrame would automatically deal with laying out any subviews of controllers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文