Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.