iPhone - UIWindow rotating depending on current orientation?

本秂侑毒 提交于 2019-12-17 17:30:59

问题


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?


回答1:


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.




回答2:


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




回答3:


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 }




回答4:


I don't know that you do anything with the window. But the root controller needs to respond to shouldAutorotate with a YES.




回答5:


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.



来源:https://stackoverflow.com/questions/6697605/iphone-uiwindow-rotating-depending-on-current-orientation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!