【发布时间】:2012-05-13 05:30:59
【问题描述】:
我正在编写一个通用应用程序。因此,我已对其进行了设置,以便我的 XIB/NIB 不是使用视图控制器创建的,而是单独创建的,然后通过将 XIB 上的类名设置为适当的视图控制器的类名来链接到视图控制器(以及将 File's Owner 上的视图链接到 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,因为这在我的通用应用程序中有所不同。
【问题讨论】:
-
这些问题甚至都不相似。同样的问题,两种完全不同的背景。