ConvertRect:toView 不返回预期的窗口坐标

发布于 2024-12-12 02:48:38 字数 451 浏览 0 评论 0原文

目前,我正在尝试获取 UIWebView 的坐标。该 webView 是 viewController 视图的子视图。 viewController 包含在 UINavigationController 内。状态栏和导航栏都在屏幕上。

方向是纵向。该视图已以模态方式呈现。

状态栏的高度为 20,导航栏的高度为 44,我预计 webView 的框架(在窗口坐标中)的原点为 (0, 64),宽度为 (320, 416)。

但是,当我运行这条线时,

CGRect frame = [webView.superview convertRect:webView.frame toView:nil];

我得到的原点为 (0, 0),宽度为 (320, 416)。

我需要做什么才能获得我期望的正确来源?我是否错误地使用了这个方法?或者由于某种原因我的期望是错误的?

Currently, I am trying to get the coordinates of a UIWebView. This webView is the subview a viewController's view. The viewController is contained inside of a UINavigationController. The status bar and the navigation bar are both on screen.

The orientation is portrait. The view has been presented modally.

With the height of status bar at 20 and the height of navigation bar at 44, I would expect the frame of the webView (in window coordinates) would have an origin at (0, 64) and a width of (320, 416).

However, when I run this line

CGRect frame = [webView.superview convertRect:webView.frame toView:nil];

I get an origin of (0, 0) and a width of (320, 416).

Any ideas what I need to do to get the correct origin I am expecting? Am I using this method incorrectly? Or are my expectations wrong for some reason?

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

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

发布评论

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

评论(1

一梦等七年七年为一梦 2024-12-19 02:48:38

这完好无损是很奇怪的。我使用一个由窗口、导航控制器、视图控制器和 Web 视图组成的非常基本的应用程序进行了测试。全部以编程方式创建。代码如下,最后我绘制了所有视图框架,而最后我绘制了转换后的矩形:


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.

    UIViewController *vc = [[UIViewController alloc] initWithNibName:nil bundle:nil];
    UIWebView *wv = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 416)];
    [wv loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]];
    [vc.view addSubview:wv];
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
    self.window.rootViewController=nc;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    NSLog(@"W: %@",NSStringFromCGRect(self.window.bounds));
    NSLog(@"NC: %@",NSStringFromCGRect(nc.view.frame));
    NSLog(@"VC: %@",NSStringFromCGRect(vc.view.frame));
    NSLog(@"WV: %@",NSStringFromCGRect(wv.frame));

    NSLog(@"WV->WIN: %@",NSStringFromCGRect([wv.superview convertRect:wv.frame toView:nil]));

    return YES;
}

结果如下:

W: {{0, 0}, {320, 480}}
NC: {{0, 0}, {320, 480}}
VC: {{0, 0}, {320, 416}}
WV: {{0, 0}, {320, 416}}
WV->WIN: {{0, 64}, {320, 416}}

这是您所期望的。您可能在视图在层次结构中的放置方式上犯了一些错误。例如,您是否使用 Interface Builder,在这种情况下您可以检查是否没有在视图位置放置错误的坐标?

This is quite strange intact. I did a test with a very basic application made of a window, a navigation controller, a view controller and a web view. All created programmatically. The code is the following and at the end I plot all view frames while at the end I plot the converted rect:


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.

    UIViewController *vc = [[UIViewController alloc] initWithNibName:nil bundle:nil];
    UIWebView *wv = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 416)];
    [wv loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]];
    [vc.view addSubview:wv];
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
    self.window.rootViewController=nc;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    NSLog(@"W: %@",NSStringFromCGRect(self.window.bounds));
    NSLog(@"NC: %@",NSStringFromCGRect(nc.view.frame));
    NSLog(@"VC: %@",NSStringFromCGRect(vc.view.frame));
    NSLog(@"WV: %@",NSStringFromCGRect(wv.frame));

    NSLog(@"WV->WIN: %@",NSStringFromCGRect([wv.superview convertRect:wv.frame toView:nil]));

    return YES;
}

The results are the following:

W: {{0, 0}, {320, 480}}
NC: {{0, 0}, {320, 480}}
VC: {{0, 0}, {320, 416}}
WV: {{0, 0}, {320, 416}}
WV->WIN: {{0, 64}, {320, 416}}

which are the one you were expecting. Probably you're doing some mistake in the way the views are placed in the hierarchy. Are you for example using Interface Builder and in such case can you check that you didn't put the wrong coordinates in the view positions?

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