Skip to content

Instantly share code, notes, and snippets.

@Shilo
Created November 10, 2011 01:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shilo/1353798 to your computer and use it in GitHub Desktop.
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.
//
// 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
//
// 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