Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

smileyborg commented Feb 26, 2015

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

This comment has been minimized.

Copy link

klaas commented Aug 20, 2015

@andrey-sikerin

This comment has been minimized.

Copy link

andrey-sikerin commented Jan 21, 2016

Hi,

Could you say me,

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

@yashwant2626

This comment has been minimized.

Copy link

yashwant2626 commented Jul 14, 2016

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

This comment has been minimized.

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
You can’t perform that action at this time.