Created
November 10, 2011 01:24
-
-
Save Shilo/1353798 to your computer and use it in GitHub Desktop.
A UIViewController subclass that allows one to easily set the allowed orientations and content orientation.
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
// | |
// OrientatedViewController.h | |
// | |
// Created by Shilo White on 10/30/11. | |
// Copyright (c) 2011 Shilocity Productions. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
typedef enum { | |
AllowedOrientationPortrait = 1 << 1, | |
AllowedOrientationPortraitUpsideDown = 1 << 2, | |
AllowedOrientationLandscapeRight = 1 << 3, | |
AllowedOrientationLandscapeLeft = 1 << 4, | |
AllowedOrientationAll = 30 | |
} AllowedOrientation; | |
@interface OrientatedViewController : UIViewController { | |
UIInterfaceOrientation _contentOrientation; | |
AllowedOrientation _allowedOrientation; | |
} | |
@property (nonatomic, assign, readwrite) AllowedOrientation allowedOrientation; | |
@property (nonatomic, assign, readwrite) UIInterfaceOrientation contentOrientation; | |
@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
// | |
// OrientatedViewController.m | |
// | |
// Created by Shilo White on 10/30/11. | |
// Copyright (c) 2011 Shilocity Productions. All rights reserved. | |
// | |
#import "OrientatedViewController.h" | |
@protocol OrientatedViewControllerSubclass <NSObject> | |
- (void)orientateContentToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration; | |
@end | |
@interface OrientatedViewController () | |
- (BOOL)shouldAllowInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation; | |
@end | |
@implementation OrientatedViewController | |
@synthesize allowedOrientation = _allowedOrientation; | |
@synthesize contentOrientation = _contentOrientation; | |
- (id)init { | |
if ((self = [super init])) { | |
_allowedOrientation = AllowedOrientationAll; | |
_contentOrientation = self.interfaceOrientation; | |
} | |
return self; | |
} | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
_contentOrientation = self.interfaceOrientation; | |
} | |
- (BOOL)shouldAllowInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { | |
return (((1<<interfaceOrientation) & _allowedOrientation) > 0); | |
} | |
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { | |
return [self shouldAllowInterfaceOrientation:interfaceOrientation]; | |
} | |
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { | |
if (_contentOrientation != toInterfaceOrientation) { | |
_contentOrientation = toInterfaceOrientation; | |
if ([self respondsToSelector:@selector(orientateContentToInterfaceOrientation:duration:)]) | |
[(id<OrientatedViewControllerSubclass>)self orientateContentToInterfaceOrientation:toInterfaceOrientation duration:duration]; | |
} | |
} | |
- (void)dealloc { | |
[super dealloc]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment