Last active
February 2, 2021 14:57
-
-
Save smileyborg/a5d1355773ad2ba6bb1e to your computer and use it in GitHub Desktop.
Migrating Rotation Code to iOS 8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
I created a Swift version: https://gist.github.com/klaas/cb8e9bbee4bdab9c59e8
Hi,
Could you say me,
It there a guide where Apple suggested to use this piece of code (UIView+Orientation) for layout migration?
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
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
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.