iPhone - UIWindow 根据当前方向旋转?
我正在向我的应用程序添加一个额外的 UIWindow。 我的主窗口可以正确旋转,但我添加的这个附加窗口不会旋转。
根据当前设备方向旋转 UIWindow 的最佳方法是什么?
I am adding an additional UIWindow to my app.
My main window rotates correctly, but this additional window I have added does not rotate.
What is the best way to rotate a UIWindow according to the current device orientation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要为 UIWindow 自己推出。
监听
UIApplicationDidChangeStatusBarFrameNotification
通知,然后在状态栏更改时设置转换。您可以从
-[UIApplication statusBarOrientation]
读取当前方向,并按如下方式计算变换:根据窗口的大小,您可能还需要更新框架。
You need to roll your own for UIWindow.
Listen for
UIApplicationDidChangeStatusBarFrameNotification
notifications, and then set the the transform when the status bar changes.You can read the current orientation from
-[UIApplication statusBarOrientation]
, and calculate the transform like this:Depending on your window´s size you might need to update the frame as well.
只需创建一个带有自己的
UIView
的UIViewController
,将其指定为rootViewController
到您的窗口,并将所有其他 UI 添加到控制器的视图(不是直接到窗口),控制器将为您处理所有旋转:当然,也可以在界面生成器中创建完全相同的设置,无需一行代码(除了之前设置帧大小以填充整个屏幕)显示窗口)。
Just create a
UIViewController
with its ownUIView
, assign it asrootViewController
to your window and add all further UI to the view of the controller (not directly to the window) and the controller will take care of all rotations for you:Of course, exactly the same setup can also be created in interface builder w/o a single line of code (except for setting frame sizes to fill the whole screen before displaying the window).
您需要设置新窗口的rootViewController。然后窗口的子视图将正确旋转。
然后您可以在旋转方法中更改框架。
例如(ios8 中的 swift)
覆盖 func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
customAlertView.frame = UIScreen.mainScreen().bounds
}
You need set new window's rootViewController.Then the window's subviews will rotate correctly.
Then you can change frames in the rotate methods.
e.g. (swift in ios8)
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
customAlertView.frame = UIScreen.mainScreen().bounds
}
我不知道你对窗户做了什么。但根控制器需要用 YES 来响应 shouldAutorotate。
I don't know that you do anything with the window. But the root controller needs to respond to shouldAutorotate with a YES.
您可以为您的 UIWindow 设置 rootController。例如:
// 'self' 将是您添加了 UIWindow 视图的 ViewController。因此,每当您 ViewController 更改方向时,您的窗口视图也会更改其方向。
如果您遇到任何问题,请告诉我。
You can set the rootController for your UIWindow. e.g:
// 'self' will the ViewController on which you had added UIWindow view. So whenever you ViewController change the orientation, your window view also change it's orientation.
Let me know if you face any issue.