Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save evadne/838743 to your computer and use it in GitHub Desktop.
Save evadne/838743 to your computer and use it in GitHub Desktop.
UIWebView Full-Screen Movie Post-Autorotation-Dismissal Layout Fix
@implementation IRWebViewController (irFullscreenMediaPlaybackAutorotationLayoutFix)
- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
// There is a bug, where launching a media player from the web view, fullscreening it, then rotating the device, and tapping on the media to see the timelime, causes the layout to go all berserk. This is a hack to combat that.
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
if ((!self.view.superview) || (self.parentViewController.modalViewController != self) || (self.modalPresentationStyle != UIModalPresentationFullScreen)) {
return;
}
UIInterfaceOrientation currentOrientation = [UIApplication sharedApplication].statusBarOrientation;
BOOL currentOrientationIsPortrait = UIInterfaceOrientationIsPortrait(currentOrientation);
CGRect windowBounds = self.view.window.bounds;
CGRect finalFrame = IRCGRectAlignToRect(
(CGRect) {
CGPointZero,
(CGSize) {
windowBounds.size.width - (currentOrientationIsPortrait ? 0 : 20),
windowBounds.size.height - (currentOrientationIsPortrait ? 20 : 0)
}
},
windowBounds,
(IRAnchor[]) {
[UIInterfaceOrientationPortrait] = irBottom,
[UIInterfaceOrientationPortraitUpsideDown] = irTop,
[UIInterfaceOrientationLandscapeLeft] = irRight,
[UIInterfaceOrientationLandscapeRight] = irLeft
}[currentOrientation],
YES
);
if (CGRectEqualToRect(finalFrame, self.view.frame))
return;
NSLog(@"Unexpected frame. Compensating.");
// Is the topmost view a fullscreen view which is the same size of the current window?
// If yes, assume that it is the media player. Recompensate its layout origin so the timeline appears at the right place
// Do that before changing our own view, to avoid UIView autoresizing from changing its frame
UIView *topmostView = [self.view.subviews objectAtIndex:([self.view.subviews count] - 1)];
CGRect convertedTopmostViewFrame = [self.view convertRect:topmostView.bounds fromView:topmostView];
CGRect capturedTopmostViewFrame = topmostView.frame;
self.view.frame = finalFrame;
if (!CGRectEqualToRect(convertedTopmostViewFrame, windowBounds))
return;
topmostView.frame = (CGRect) { (CGPoint) { 0, -20 }, capturedTopmostViewFrame.size };
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment