奇怪的UIView坐标问题 [英] Weird UIView coordinate issue

查看:38
本文介绍了奇怪的UIView坐标问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写通用应用程序.结果,我将其设置为不使用视图控制器创建我的XIB/NIB,而是将其单独创建,然后通过将XIB上的类名设置为适当的视图控制器的类名来链接到视图控制器.将文件所有者"上的视图链接到XIB上的视图).然后,在初始化视图控制器时,我会加载适当的XIB/NIB(iPhone或iPad),如下所示:

I am writing a universal application. As a result I have set it up so that my XIB/NIBs are not created with the view controllers, but are created separately and then linked to the view controller by setting the Class Name on the XIB to that of the appropriate view controller (and linking the view on File's Owner to the view on the XIB). I then load the appropriate XIB/NIB (iPhone or iPad) when the view controller is initialized, like so:

SomeViewController *vc = [[SomeViewController alloc] initWithNibName:@"SomeView-iPhone" bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
[vc release];

此外,iphone版本的设置是在底部的UITabBarController和在顶部的UINavigationController.这样创建的:

Additionally, the iphone version is setup with a UITabBarController at the bottom and a UINavigationController at the top. That is created like this:

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视图显示良好(带有工作选项卡栏和导航栏),导航正常.这是事情变得奇怪的地方(两条龙很奇怪).

All that works perfectly fine. The iPhone views are displayed fine (with working tab bar and nav bar) and navigation works fine. Here's where things get weird (two dragons weird).

-位于IB中的大多数UI元素(标签,按钮等)显示良好
-如果我在代码中创建一个UIScrollView并将其添加到视图中,它将很好显示
-如果我在IB中创建UIScrollView并将其放置在视图上,则它在运行时会从设计时在IB中出现的位置开始向上转置93像素
-如果我在IB中将UIScrollView创建为单独的视图,然后以代码形式将其添加到主视图中,则它会向上移位93像素.

-Most UI elements (labels, buttons, etc) that are positioned in IB show up fine
-If I create a UIScrollView in code and add it to the view, it shows up fine
-If I create a UIScrollView in IB and put it on the view, it gets transposed up by 93 pixels at run time from where it appears in IB at design time
-If I create a UIScrollView in IB as a separate view and then add it to the main view in code it gets transposed up by 93 pixels.

换句话说,如果我这样做:

In other words, if I do this:

[scrollView setFrame:CGRectMake(0,0,320,100)];
[self.view addSubview:scrollView]; 

然后,对于在代码中创建的UIScrollView,它将显示为0,0;对于在IB中创建(并通过IBOutlet链接到ivar)的UIScrollView,它将显示为(0,-93).现在恰好发生这样的情况,即93 = 44 + 49,这是制表符栏和导航栏的总和.这显然不是巧合.

Then it would show up at 0,0 for a UIScrollView that was created in code, but it would show up at (0,-93) for a UIScrollView created in IB (and linked to the ivar via an IBOutlet). Now it just so happens that 93 = 44 + 49, which is the height of the tab bar and nav bar combined. That is clearly not a coincidence.

问题是,为什么iOS会在运行时将导航栏和选项卡栏的高度(而不是UILabel或UIButton等其他UI元素)的高度上移到IB中创建的滚动视图?

So the question is, why is iOS moving a scroll view created in IB up by the height of the nav bar and tab bar at runtime (but not other UI elements like UILabel or UIButton)?

我之前在仅iPhone的应用程序中使用过设置标签栏和导航栏的代码,但没有这个问题,因此我怀疑这与使用initWithNibName初始化我的事情有关.查看控制器或将XIB文件手动链接到VC,因为这与我的通用应用程序不同.

I have used the code that sets up the tab bar and nav bar before in an iPhone only app, and it didn't have this issue, so I suspect that this has something to do with using initWithNibName to initialize my view controllers or the manual linking of the XIB files to the VC, as that is what is different in my universal app.

推荐答案

我解决了.我偶然发现了能解决问题的这个问题/答案:

I solved it. I stumbled across this question/answer which did the trick:

UIView尺寸/位置在iPad上无法正常运行在iPhone上

之所以发生这种情况,是因为默认情况下,在IB中创建的滚动视图被设置为自动调整到左下角.我将其切换为绑定到左上角,现在一切正常.奇怪的.

This was happening because by default the scroll view created in IB was set to autosizing to the lower left corner. I switched it to bind to the top left corner, and now all is well. Strange.

这篇关于奇怪的UIView坐标问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆