我正在编写一个通用应用程序。因此,我进行了设置,以便我的 XIB/NIB 不是使用 View Controller 创建的,而是单独创建的,然后通过将 XIB 上的类名设置为相应 View Controller 的类名来链接到 View Controller (并且将文件所有者的 View 链接到 XIB 上的 View )。然后,当 View Controller 初始化时,我加载适当的 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 View 显示良好(带有工作选项卡栏和导航栏)并且导航工作正常。这就是事情变得奇怪的地方(两条龙很奇怪)。
-位于 IB 中的大多数 UI 元素(标签、按钮等)显示良好
-如果我在代码中创建 UIScrollView 并将其添加到 View 中,它会显示良好
-如果我在 IB 中创建一个 UIScrollView 并将其放在 View 上,则它会在运行时从设计时在 IB 中出现的位置向上移调 93 像素
-如果我在 IB 中创建一个 UIScrollView 作为单独的 View ,然后将其添加到代码中的主视图,它将向上移动 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 中创建的 ScrollView 向上移动导航栏和选项卡栏的高度(而不是其他 UI 元素,如 UILabel 或 UIButton)?
编辑:我之前在仅限 iPhone 的应用程序中使用过设置选项卡栏和导航栏的代码,并且没有这个问题,所以我怀疑这与使用 initWithNibName 初始化我的应用程序有关查看 Controller 或将 XIB 文件手动链接到 VC,因为这是我的通用应用程序中的不同之处。
最佳答案
我解决了。我偶然发现了这个问题/答案,它起到了作用:
UIView size/position not working on iPad as on iPhone
发生这种情况是因为默认情况下,在 IB 中创建的 ScrollView 被设置为自动调整大小到左下角。我将其切换为绑定(bind)到左上角,现在一切都很好。奇怪的。
关于iphone - 奇怪的 UIView 坐标问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10435814/