iPad 方向/框架尺寸问题
在“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用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.