Can I rotate a UIView without the black bars?

后端 未结 4 1526
后悔当初
后悔当初 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:26

    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;
                }
            }
        }
    }
    

提交回复
热议问题