iPhone - UIWindow 根据当前方向旋转?

发布于 2024-11-24 06:36:17 字数 101 浏览 5 评论 0原文

我正在向我的应用程序添加一个额外的 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 技术交流群。

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

发布评论

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

评论(5

日久见人心 2024-12-01 06:36:18

您需要为 UIWindow 自己推出。

监听 UIApplicationDidChangeStatusBarFrameNotification 通知,然后在状态栏更改时设置转换。

您可以从 -[UIApplication statusBarOrientation] 读取当前方向,并按如下方式计算变换:

#define DegreesToRadians(degrees) (degrees * M_PI / 180)

- (CGAffineTransform)transformForOrientation:(UIInterfaceOrientation)orientation {

    switch (orientation) {

        case UIInterfaceOrientationLandscapeLeft:
            return CGAffineTransformMakeRotation(-DegreesToRadians(90));

        case UIInterfaceOrientationLandscapeRight:
            return CGAffineTransformMakeRotation(DegreesToRadians(90));

        case UIInterfaceOrientationPortraitUpsideDown:
            return CGAffineTransformMakeRotation(DegreesToRadians(180));

        case UIInterfaceOrientationPortrait:
        default:
            return CGAffineTransformMakeRotation(DegreesToRadians(0));
    }
}

- (void)statusBarDidChangeFrame:(NSNotification *)notification {

    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

    [self setTransform:[self transformForOrientation:orientation]];

}

根据窗口的大小,您可能还需要更新框架。

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:

#define DegreesToRadians(degrees) (degrees * M_PI / 180)

- (CGAffineTransform)transformForOrientation:(UIInterfaceOrientation)orientation {

    switch (orientation) {

        case UIInterfaceOrientationLandscapeLeft:
            return CGAffineTransformMakeRotation(-DegreesToRadians(90));

        case UIInterfaceOrientationLandscapeRight:
            return CGAffineTransformMakeRotation(DegreesToRadians(90));

        case UIInterfaceOrientationPortraitUpsideDown:
            return CGAffineTransformMakeRotation(DegreesToRadians(180));

        case UIInterfaceOrientationPortrait:
        default:
            return CGAffineTransformMakeRotation(DegreesToRadians(0));
    }
}

- (void)statusBarDidChangeFrame:(NSNotification *)notification {

    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

    [self setTransform:[self transformForOrientation:orientation]];

}

Depending on your window´s size you might need to update the frame as well.

故乡的云 2024-12-01 06:36:18

只需创建一个带有自己的 UIViewUIViewController,将其指定为 rootViewController 到您的窗口,并将所有其他 UI 添加到控制器的视图(不是直接到窗口),控制器将为您处理所有旋转:

UIApplication * app = [UIApplication sharedApplication];
UIWindow * appWindow = app.delegate.window;

UIWindow * newWindow = [[UIWindow alloc] initWithFrame:appWindow.frame];
UIView * newView = [[UIView alloc] initWithFrame:appWindow.frame];
UIViewController * viewctrl = [[UIViewController alloc] init];

viewctrl.view = newView;
newWindow.rootViewController = viewctrl;

// Now add all your UI elements to newView, not newWindow.
// viewctrl takes care of all device rotations for you.

[newWindow makeKeyAndVisible];
// Or just newWindow.hidden = NO if it shall not become key

当然,也可以在界面生成器中创建完全相同的设置,无需一行代码(除了之前设置帧大小以填充整个屏幕)显示窗口)。

Just create a UIViewController with its own UIView, assign it as rootViewController 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:

UIApplication * app = [UIApplication sharedApplication];
UIWindow * appWindow = app.delegate.window;

UIWindow * newWindow = [[UIWindow alloc] initWithFrame:appWindow.frame];
UIView * newView = [[UIView alloc] initWithFrame:appWindow.frame];
UIViewController * viewctrl = [[UIViewController alloc] init];

viewctrl.view = newView;
newWindow.rootViewController = viewctrl;

// Now add all your UI elements to newView, not newWindow.
// viewctrl takes care of all device rotations for you.

[newWindow makeKeyAndVisible];
// Or just newWindow.hidden = NO if it shall not become key

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).

北方。的韩爷 2024-12-01 06:36:18

您需要设置新窗口的rootViewController。然后窗口的子视图将正确旋转。

myNewWindow!.rootViewController = self

然后您可以在旋转方法中更改框架。

例如(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.

myNewWindow!.rootViewController = self

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
}

月棠 2024-12-01 06:36:18

我不知道你对窗户做了什么。但根控制器需要用 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.

烟织青萝梦 2024-12-01 06:36:18

您可以为您的 UIWindow 设置 rootController。例如:

fileprivate(set) var bottonOverlayWindow = UIWindow()

self.bottonOverlayWindow.rootViewController = self; 

// 'self' 将是您添加了 UIWindow 视图的 ViewController。因此,每当您 ViewController 更改方向时,您的窗口视图也会更改其方向。

如果您遇到任何问题,请告诉我。

You can set the rootController for your UIWindow. e.g:

fileprivate(set) var bottonOverlayWindow = UIWindow()

self.bottonOverlayWindow.rootViewController = self; 

// '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.

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