为什么我的视图在界面方向更改时无法正确定位?

发布于 2024-10-06 21:28:47 字数 1216 浏览 5 评论 0原文

我里面有一个 UIViewController 和一个 UIWebView 。我希望 UIWebView 在横向和纵向模式下位于 iPad 屏幕的中央。所以,我是这样实现的,

// UIViewController
// InfoGraphicView is the UIWebView

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
                                duration:(NSTimeInterval)duration {
    if (toInterfaceOrientation == UIInterfaceOrientationPortrait ||
        toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        [self layoutPortrait];
    } else {
        [self layoutLandscape];
    }
}

- (void)layoutLandscape {
    NSLog(@"Layout Landscape");
    infoGraphicView.frame = CGRectMake(100, 100, 936, 700);
}

- (void)layoutPortrait {
    NSLog(@"Layout Portrait");
    infoGraphicView.frame = CGRectMake(100, 100, 700, 936);
}

但是,它的行为并不像我预期的那样。在上面的代码中,我期望 UIWebView 距顶部和左侧 100px(或点或任何单位)。但事实并非如此。在纵向模式下,它与屏幕左上角齐平,而在横向模式下,它似乎部分位于屏幕外的左上角。

如果我将框架设置为 CGRectMake(-100, 100, 700, 936) 然后我会将其放置在屏幕的中心,就像我希望的那样,但我不知道为什么。

像往常一样,很可能有一些简单的事情我忽略了,但我无法弄清楚。一如既往,我们非常感谢任何帮助。

I have a UIViewController one UIWebView in it. I'd like the UIWebView to be positioned in the centre of the iPad screen in landscape and portrait modes. So, I've implemented it like this

// UIViewController
// InfoGraphicView is the UIWebView

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
                                duration:(NSTimeInterval)duration {
    if (toInterfaceOrientation == UIInterfaceOrientationPortrait ||
        toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        [self layoutPortrait];
    } else {
        [self layoutLandscape];
    }
}

- (void)layoutLandscape {
    NSLog(@"Layout Landscape");
    infoGraphicView.frame = CGRectMake(100, 100, 936, 700);
}

- (void)layoutPortrait {
    NSLog(@"Layout Portrait");
    infoGraphicView.frame = CGRectMake(100, 100, 700, 936);
}

However, it's not behaving as I expected. In the above code, I would expectt he UIWebView to be 100px (or points or whatever the unit is) away from the top and the left. But it's not. In Portrait mode it appears flush with the top left of the screen, and in Landscape mode it seems to be partially offscreen in the top left.

If I set the frame as CGRectMake(-100, 100, 700, 936) then I get it positioned in the center of the screen as I'd like it to be, but I've no idea why.

As usual, there's most likely something simple I'm overlooking but I can't figure it out. Any help greatly appreciated as always.

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

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

发布评论

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

评论(2

江城子 2024-10-13 21:28:47

您在 infoGraphicView 上设置的坐标是相对于其超级视图的,而不是相对于屏幕的。并且视图不一定会裁剪它们的子视图。此外,自动设置为 self.view 的形状将取决于 Interface Builder 中设置的缩放标志。但是,我认为默认情况下它被设置为填充整个屏幕。

也就是说,我认为错误在于您使用 willRotateToInterfaceOrientation:duration: 。这是在旋转开始之前调用的,因此 self.view 具有旧的尺寸(即,如果从纵向旋转到横向,它仍然是纵向尺寸,反之亦然)。可能更好的是挂接 willAnimateRotationToInterfaceOrientation:duration: - 然后正确的大小已经设置,你将在 CoreAnimation 块内,这样你的视图将作为旋转动画的一部分增长/收缩。

还值得检查您在 infoGraphicView 上设置的调整大小标志。除了您所做的任何更改之外,它们也会自动生效。所以你可能想禁用它们。

The coordinates you set on infoGraphicView are relative to its superview, not to the screen generally. And views don't necessarily clip their subviews. Furthermore, the shape set automatically to self.view will depend on the scaling flags set in Interface Builder. However, I think that by default it is set to fill the whole screen.

That said, I think the mistake is in your use of willRotateToInterfaceOrientation:duration:. That is called before the rotation begins, so self.view has the old size (ie, it'll still be portrait sized if rotating from portrait to landscape and vice versa). Probably better to hook willAnimateRotationToInterfaceOrientation:duration: — then the correct size has been set and you'll be within the CoreAnimation block so your view will grow/shrink as part of the rotation animation.

It's also worth checking which resizing flags you have set on infoGraphicView. They'll take effect automatically, in addition to any changes you make. So you probably want to disable them all.

清风无影 2024-10-13 21:28:47

这可能是 Web 视图所在视图的问题。使用的坐标系是视图的超级视图的坐标系。如果该视图没有在旋转时调整大小,那么您将看到像这样的意外布局。可以通过superview属性访问视图的superview;查看其框架的一种方法是使用其描述。将此行放入您的布局方法之一中:

NSLog(@"Superview: %@", [infoGraphicView superview]);

这应该打印出视图的描述。

弄清楚这一点后,如果您希望 Web 视图具有相同的布局,则可以使用其 autoresizingMask 属性。如果你这样设置:

infoGraphicView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

那么视图会自动改变它的宽度和高度,以保持上、左、右、下边距相同。

This probably is an issue with the view that the web view is in. The coordinate system used is that of the view’s superview. If that view isn’t being resized on rotation, then you’ll see unexpected layout like this. You can access the superview of a view through the superview property; one way to see its frame would be to use its description. Put this line in one of your layout methods:

NSLog(@"Superview: %@", [infoGraphicView superview]);

That should print out a description of the view.

Once you get that figured out, if you want the web view to have the same layout, you can use its autoresizingMask property. If you set it like this:

infoGraphicView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

Then the view will automatically change its width and height to keep the top, left, right, and bottom margins the same.

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