Can I rotate a UIView without the black bars?

后端 未结 4 1516
后悔当初
后悔当初 2020-12-11 09:51

I have a UIView that takes up the entirety of the window on the iphone. When it rotates to landscape, the edges get the little black bars until it snaps into place, as i wou

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-11 10: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];
    }
    

提交回复
热议问题