纵向模式下的横向框架
当设备/界面处于纵向模式时,是否可以检索 UIView 在横向模式下具有的框架?如果是这样,这是如何完成的?
Is it possible to retrieve the frame a UIView will have in landscape mode, whilst the device/interface is in portrait mode? If so, how is this done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
MrThys,您应该知道您的视图应该如何,无论是纵向还是横向模式。之后,您可以将它们定义为如下常量:
然后,您可以在设备更改方向时使用这些值。 :)
MrThys, you should know how your view should be, either in portrait or landscape mode. After that, you can define them as constants like this:
You could then use these values when your device changes orientation. :)
并不真地。您知道屏幕的尺寸只会交换宽度和高度。但是,如果状态栏可见,则可用区域将有所不同(例如,iPhone 上的纵向 320x460 与横向 300x480),然后如果您使用 UINavigationController 或类似设备,则必须考虑其镶边,然后,如果您的视图不是容器视图控制器中视图控制器的顶级视图,您还必须考虑各种自动调整大小蒙版和任何自定义布局代码。
为什么你甚至需要这个?一般来说,您应该只响应 UIViewController 的 willRotateToInterfaceOrientation:duration: 或 UIView 的layoutSubviews 中指定的大小。
Not really. You know the dimensions of the screen will just swap the width and height. But if the status bar is visible the usable area will be different (e.g. 320x460 in portrait versus 300x480 in landscape on an iPhone), and then if you're in a UINavigationController or the like you'll have to take its chrome into account, and then if your view isn't the top-level view of the view controller in the container view controller you have to take into account the various autoresizing masks and any custom layout code too.
Why do you even need this? You should in general just respond to the size you're given in UIViewController's
willRotateToInterfaceOrientation:duration:
or UIView'slayoutSubviews
.我不知道有什么可靠的方法可以做到这一点。在我的一个项目中,我使用了 UIApplicationWillChangeStatusBarOrientationNotification。查询视图的
frame
或bounds
属性会返回不一致的结果,具体取决于视图是否被另一个视图覆盖(也可能存在其他干扰情况)。也许willRotateToInterfaceOrientation:
方法内部的情况有所不同。就我而言,我最终实现了自己的方法来找出视图边界:
在您的情况下,您可以尝试在从笔尖加载视图框架后找出视图框架。然后,要么推断框架在横向方向上的形状,要么在界面生成器中显式设置框架并在两个方向的代码中复制它。
I don't know of a reliable way to do this. In one of my projects I've used
UIApplicationWillChangeStatusBarOrientationNotification
. Querying eitherframe
orbounds
property of the view returns inconsistent results depending on wether the view is covered by another view (there may be other disrupting cases as well). Maybe the situation is different inside thewillRotateToInterfaceOrientation:
method.In my case, I ended up implementing my own method to find out the view bounds:
In your case, you could try to find out the view frame once it's been loaded from the Nib. Then, either deduce what the frame will be in landscape orientation or explicitly set the frame in the Interface Builder and duplicate it in the code for both orientations.