iPad 方向/框架尺寸问题

发布于 2024-09-24 18:22:36 字数 828 浏览 4 评论 0原文

在“View Did Load”中,我试图确定视图的大小,以便我可以适当地调整子视图的大小。我希望它始终拉伸屏幕的长度和宽度,无论方向如何。

quest *anview = [[quest alloc] initWithFrame: CGRectMake(50, 50, self.view.frame.size.width-100, self.view.frame.size.height-100)];
self.aquest=anview;

但是,当我在横向时它应该返回相反的值时,它总是返回 748 的宽度和 1024 的高度。我知道您无法在模拟器上获得方向,但这也会发生在设备上。当我获得方向时:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
NSLog(@"device orientation:%d",orientation);

我正确地获得了方向,但无论方向如何,此日志语句都会返回 748/1024。

    NSLog(@"size w:%f",self.view.frame.size.width);
    NSLog(@"size h:%f",self.view.frame.size.height);

有人知道发生了什么事吗?这与我将其放入 viewdidLoad 方法有关吗?我应该把它放在 viewWillAppear 还是 viewDidAppear 中?

In "View Did Load" I'm trying to determine the size of the view so I can appropriately size a subview. I want it to always stretch about the length and width of the screen regardless of orientation.

quest *anview = [[quest alloc] initWithFrame: CGRectMake(50, 50, self.view.frame.size.width-100, self.view.frame.size.height-100)];
self.aquest=anview;

But this always returns a width of 748 and a height of 1024 when it should return the opposite when I'm in landscape. I know that you can't get orientation on the simulator but this occurs on the device as well. When I get the orientation:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
NSLog(@"device orientation:%d",orientation);

I correctly get the orientation but this log statement returns the 748/1024 regardless of orientation.

    NSLog(@"size w:%f",self.view.frame.size.width);
    NSLog(@"size h:%f",self.view.frame.size.height);

Anyone know what's going on? Does this have to do with me putting this in the viewdidLoad method? Should I put it in viewWillAppear or viewDidAppear?

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

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

发布评论

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

评论(1

娇纵 2024-10-01 18:22:36

尝试使用bounds属性而不是frame

即使您以横向模式启动应用程序,视图也始终以纵向模式创建,然后旋转。您应该能够通过将子视图的大小相对于父级的纵向大小和自动缩放蒙版设置为 UIViewAutoresizingMaskFlexibleWidth|UIViewAutoresizingFlexibleHeight 来保持子视图的拉伸状态,前提是 shouldAutorotateToInterfaceOrientation: 返回 YES正确的设备方向。它还会保持与上边距和左边距的距离,除非您向蒙版添加不同的UIViewAutoresizingFlexibleMargin

Try to use the bounds property instead of frame.

The view is always created in portrait mode and then rotated, even if you launch the application in landscape. You should be able to keep the subview stretched by setting its size relative to the parent's portrait size and the autorsizing mask to UIViewAutoresizingMaskFlexibleWidth|UIViewAutoresizingFlexibleHeight, providing the shouldAutorotateToInterfaceOrientation: return YES for the right device orientation. It will also keep the distance from the top and left margin unless you add to the mask a different UIViewAutoresizingFlexibleMargin.

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