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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I find status bar orientation to be unreliable. The height , width comparison seems to be best way to do this.