我正在编写一个通用应用程序。因此,我进行了设置,以便我的 XIB/NIB 不是使用视图控制器创建的,而是单独创建的,然后通过将 XIB 上的类名设置为相应视图控制器的类名来链接到视图控制器(并且将文件所有者的视图链接到 XIB 上的视图)。然后,当视图控制器初始化时,我加载适当的 XIB/NIB(iPhone 或 iPad),如下所示:
SomeViewController *vc = [[SomeViewController alloc] initWithNibName:@"SomeView-iPhone" bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
[vc release];
此外,iPhone 版本设置了底部的 UITabBarController 和顶部的 UINavigationController。它是这样创建的:
MyAppDelegate *appDelegate = (MyAppDelegate *)[UIApplication sharedApplication].delegate;
UINavigationController *localNavigationController;
UITabBarController *tabBarController = [[UITabBarController alloc] init];
NSMutableArray *localControllersArray = [[NSMutableArray alloc] init];
//setup the first tab
FirstViewController *vc = [[FirstViewController alloc] initWithNibName:@"FirstView-iPhone" bundle:nil];
localNavigationController = [[UINavigationController alloc] initWithRootViewController:vc];
[localControllersArray addObject:localNavigationController];
[localNavigationController release];
[vc release];
//setup the second tab
SecondViewController *vc = [[SecondViewController alloc] initWithNibName:@"SecondView-iPhone" bundle:nil];
localNavigationController = [[UINavigationController alloc] initWithRootViewController:vc];
[localControllersArray addObject:localNavigationController];
[localNavigationController release];
[vc release];
....
//set the controllers onto the tab bar
tabBarController.viewControllers = localControllersArray;
[localControllersArray release];
[appDelegate.window setRootViewController:tabBarController];
[tabBarController release];
一切都很好。 iPhone 视图显示良好(带有工作选项卡栏和导航栏)并且导航工作正常。这就是事情变得奇怪的地方(两条龙很奇怪)。
-位于 IB 中的大多数 UI 元素(标签、按钮等)显示良好
-如果我在代码中创建 UIScrollView 并将其添加到视图中,它会显示正常
-如果我在 IB 中创建一个 UIScrollView 并将其放在视图上,则它会在运行时从设计时在 IB 中出现的位置向上移调 93 像素
-如果我在 IB 中创建一个 UIScrollView 作为单独的视图,然后将其添加到代码中的主视图,它将向上移动 93 像素。
换句话说,如果我这样做:
[scrollView setFrame:CGRectMake(0,0,320,100)];
[self.view addSubview:scrollView];
然后,对于在代码中创建的 UIScrollView,它将显示在 0,0,但对于在 IB 中创建的 UIScrollView(并通过 IBOutlet 链接到 ivar),它将显示在 (0,-93)。现在碰巧 93 = 44 + 49,这是标签栏和导航栏的高度之和。这显然不是巧合。
所以问题是,为什么 iOS 在运行时将 IB 中创建的滚动视图向上移动导航栏和选项卡栏的高度(而不是其他 UI 元素,如 UILabel 或 UIButton)?
编辑:我之前在仅 iPhone 的应用程序中使用过设置选项卡栏和导航栏的代码,并且它没有这个问题,所以我怀疑这与使用 initWithNibName 初始化我的视图控制器或将 XIB 文件手动链接到 VC,因为这是我的通用应用程序中的不同之处。