UIWindow 的 UIView 子视图不旋转
我正在将一个 UIView
添加到一个不是 keyWindow 的 UIWindow
中。我希望 UIView 在设备旋转时旋转。 我需要设置窗口或视图上的任何特殊属性吗?当前视图不旋转。
我知道只有应用程序的 keyWindow
的第一个子视图被告知有关设备旋转的信息。作为测试,我将视图添加到 keyWindow 的第一个子视图中。这会导致视图旋转。然而,由于各种美观原因,作为 keyWindow
第一个子视图的子视图的视图将无法工作。
另一种方法是观察视图中设备方向的变化并自己编写旋转代码。不过,如果可能的话,我想避免编写这段代码(在我看来,有一个额外的窗口更干净)。
I'm adding a UIView
to a UIWindow
that's not the keyWindow. I'd like the UIView
to rotate when the device rotates. Any special properties on the window or the view I need to set? Currently the view is not rotating.
I'm aware of the fact that only the first subview of the application's keyWindow
is told about device rotations. As a test I added my view to the first subview of the keyWindow
. This causes the view to rotate. However the view being a subview of the keyWindow
's first subview won't work for various aesthetic reasons.
An alternative approach is observing device orientation changes in the view and writing the rotation code myself. However I'd like to avoid writing this code if possible (having an additional window is cleaner in my opinion).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
UIView 不处理旋转,UIViewController 处理。
因此,您所需要做的就是创建一个 UIViewController,它实现 shouldAutorotateToInterfaceOrientation 并将此控制器设置为 UIWindow 的 rootViewController
类似的东西:
我使用了这个 MyViewController,因为我希望它反映主应用程序的更改
,但您始终可以返回如果您愿意,可以选择任何方向或写下您的逻辑。
UIView doesn't handle rotations, UIViewController does.
So, all you need is to create a UIViewController, which implements shouldAutorotateToInterfaceOrientation and sets this controller as a rootViewController to your UIWindow
Something like that:
and I used this MyViewController, cause I want it to reflect changes of main application
but you can always just return YES for any orientation or write your logic, if you wish.
您可以将以下代码添加到您的项目中
并创建了处理它的函数。
You can add below code to your project
and created the function to handle it.