Can I rotate a UIView without the black bars?

蓝咒 提交于 2019-11-28 12:44:05

You need to do two things to make this happen.

First, the window's root view controller will always resize its view to the size of the window. So your big view needs to be a subview of the root view controller's view (to keep it from being resized down), and your root view controller's view needs to have clipsToBounds set to NO. In fact all ancestors of the big view need to have clipsToBounds set to NO.

Second, when the window rotates, it gives itself black subviews to explicitly hide any views that would otherwise appear outside the window's bounds. It places these black subviews in front of its root view controller's view. You need to move your root view controller's view to the front, like this:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    UIWindow *window = self.view.window;
    [window bringSubviewToFront:window.rootViewController.view];
}

I found an alternate way that works when you have two UIWindows overlapping. (This might be the case if you want non-rotatable content in the background, with a rotatable interface floating on top.) The idea is to directly search for the black bar subviews in the frontmost UIWindow when willAnimateRotationToInterfaceOrientation is called, and explicitly hide them. The following code searches two levels deep:

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration {
    UIWindow* window = self.view.window;
    for (UIView* view in [window subviews]) {
        if (!CGRectIntersectsRect(window.bounds, view.frame)) {
            view.hidden = YES;
        }

        for (UIView* view_ in [view subviews]) {
            CGRect frame = [view_ convertRect:view_.frame toView:window];
            if (!CGRectIntersectsRect(window.bounds, frame)) {
                view_.hidden = YES;
            }
        }
    }
}

I'm currently working on an app that requires the screen to rotate at a slower than standard speed. In order to stop the black rotation rectangle clipping the view (at the standard rotation speed) before my own rotation began I simply checked the box next to General -> Deployment Info -> Requires Full Screen.

I do also have the status bar hidden on the view controller although the hide status bar box isn't checked.

UINavigationController and UITabBarController both have system subviews (UINavigationController: UINavigationTransitionView; UITabBarController: UITransitionView and UIViewControllerWrapperView) that have layer.masksToBounds set to YES by default. In order to eliminate the black edges during rotation, you must set these and all other hierarchical view's layer.masksToBounds to NO.

However, I'm not sure what the effect of altering the above-mentioned system views' layers is, so I recommend resetting their layer.masksToBounds back to YES once you are done animating.

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