Skip to content

Instantly share code, notes, and snippets.

@sunjun
Created December 24, 2014 08:09
Show Gist options
  • Save sunjun/8580b588b6946840e405 to your computer and use it in GitHub Desktop.
Save sunjun/8580b588b6946840e405 to your computer and use it in GitHub Desktop.
relayout when UIDeviceOrientationDidChangeNotification fired
- (CGRect)getScreenFrameForCurrentOrientation {
return [self getScreenFrameForOrientation:[UIApplication sharedApplication].statusBarOrientation];
}
- (CGRect)getScreenFrameForOrientation:(UIInterfaceOrientation)orientation {
UIScreen *screen = [UIScreen mainScreen];
CGRect fullScreenRect = screen.bounds;
BOOL statusBarHidden = [UIApplication sharedApplication].statusBarHidden;
//implicitly in Portrait orientation.
if(orientation == UIInterfaceOrientationLandscapeRight || orientation == UIInterfaceOrientationLandscapeLeft){
CGRect temp = CGRectZero;
temp.size.width = fullScreenRect.size.height;
temp.size.height = fullScreenRect.size.width;
fullScreenRect = temp;
} else if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
// fullScreenRect =
}
// if(!statusBarHidden){
// CGFloat statusBarHeight = 20;//Needs a better solution, FYI statusBarFrame reports wrong in some cases..
// fullScreenRect.size.height -= statusBarHeight;
// }
return fullScreenRect;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didRotate:)
name:@"UIDeviceOrientationDidChangeNotification"
object:nil];
}
- (void) didRotate:(NSNotification *)notification
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
CGSize sizeOfScreen;
CGRect frame;
switch (orientation) {
case UIDeviceOrientationPortrait:
case UIDeviceOrientationPortraitUpsideDown:
// do something for portrait orientation
frame = [self getScreenFrameForCurrentOrientation];
self.moboPlayer.moboDisplayView.frame = frame;
break;
case UIDeviceOrientationLandscapeLeft:
case UIDeviceOrientationLandscapeRight:
// do something for landscape orientation
sizeOfScreen = [[UIScreen mainScreen] bounds].size;
frame = [self getScreenFrameForCurrentOrientation];
self.moboPlayer.moboDisplayView.frame = frame;
break;
default:
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment