Skip to content

Instantly share code, notes, and snippets.

@smileyborg
Last active February 2, 2021 14:57
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save smileyborg/a5d1355773ad2ba6bb1e to your computer and use it in GitHub Desktop.
Save smileyborg/a5d1355773ad2ba6bb1e to your computer and use it in GitHub Desktop.
Migrating Rotation Code to iOS 8
//
// UIView+Orientation.h
//
#import <UIKit/UIKit.h>
// These macros should only be used if you MUST know the interface orientation for the device itself, for example when displaying a new UIWindow.
// This should be very rare; generally you should only look at the immediate parent view's size (or "view orientation" using the category below).
#define StatusBarOrientationIsPortrait UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])
#define StatusBarOrientationIsLandscape UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])
typedef NS_ENUM(NSInteger, ViewOrientation) {
ViewOrientationPortrait,
ViewOrientationLandscape
};
@interface UIView (Orientation)
/** Returns the "orientation" of size. width > height is considered "landscape", otherwise "portrait" */
+ (ViewOrientation)viewOrientationForSize:(CGSize)size;
/** Returns the "orientation" of this view based on its size. width > height is considered "landscape", otherwise "portrait" */
- (ViewOrientation)viewOrientation;
/** Returns YES if this view's height >= width */
- (BOOL)isViewOrientationPortrait;
/** Returns YES if this view's width > height */
- (BOOL)isViewOrientationLandscape;
@end
//
// UIView+Orientation.m
//
@implementation UIView (Orientation)
+ (ViewOrientation)viewOrientationForSize:(CGSize)size {
return (size.width > size.height) ? ViewOrientationLandscape : ViewOrientationPortrait;
}
- (ViewOrientation)viewOrientation {
return [[self class] viewOrientationForSize:self.bounds.size];
}
- (BOOL)isViewOrientationPortrait {
return [self viewOrientation] == ViewOrientationPortrait;
}
- (BOOL)isViewOrientationLandscape {
return [self viewOrientation] == ViewOrientationLandscape;
}
@end
@smileyborg
Copy link
Author

See this answer on Stack Overflow for more context: http://stackoverflow.com/a/27409619/796419
Also note that you may want to add a class prefix to the types and methods in this category for proper namespacing.

@klaas
Copy link

klaas commented Aug 20, 2015

@andrey-sikerin
Copy link

Hi,

Could you say me,

It there a guide where Apple suggested to use this piece of code (UIView+Orientation) for layout migration?

@yashwant2626
Copy link

i am using Width Regular & Height Regular Size Classes in iOS 9 using Autolayout and i need to load different storyboard for IPad Landscape & IPad Portrait only for three ViewController and my other screen are working fine in both the orientation. so i would your any suggestion what should i do for these three ViewController. Thanks

@scm-ns
Copy link

scm-ns commented Jan 6, 2018

I find status bar orientation to be unreliable. The height , width comparison seems to be best way to do this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment