Skip to content

Instantly share code, notes, and snippets.

@millenomi
Created April 7, 2010 13:27
Show Gist options
  • Save millenomi/358869 to your computer and use it in GitHub Desktop.
Save millenomi/358869 to your computer and use it in GitHub Desktop.
//
// ILViewController.h
// ILViewController
//
// Created by ∞ on 07/04/10.
//
// The contents of this file are in the public domain.
#import <UIKit/UIKit.h>
enum {
kILRotateIdiomDefault = 0, // setting to this will really pick the 'default' one below.
kILRotatePortrait, // portrait. on iPhone, straight up only. on iPad, both portrait orientations.
// default on iPhone.
kILRotateLandscape, // both landscape orientations
kILRotateAny, // on iPhone, any except upside down. on iPad, any orientation.
// default on iPad.
};
typedef NSInteger ILRotationStyle;
@interface ILViewController : UIViewController {
ILRotationStyle rotationStyle;
BOOL changesNavigationBarStyle;
UIBarStyle navigationBarStyle;
UIColor* navigationBarTintColor;
BOOL navigationBarTranslucent;
BOOL changesStatusBarStyle;
UIStatusBarStyle statusBarStyle;
BOOL restoresNavigationBarStyleOnDisappearing, hasOlderNavigationBarStyle;
UIBarStyle oldNavigationBarStyle;
UIColor* oldNavigationBarTintColor;
BOOL oldNavigationBarTranslucent;
BOOL restoresStatusBarStyleOnDisappearing, hasOlderStatusBarStyle;
UIStatusBarStyle oldStatusBarStyle;
}
@property ILRotationStyle rotationStyle;
@property(nonatomic, retain) IBOutlet UINavigationItem* navigationItem;
@property BOOL changesNavigationBarStyle;
@property UIBarStyle navigationBarStyle;
@property(retain) UIColor* navigationBarTintColor;
@property BOOL navigationBarTranslucent;
@property BOOL restoresNavigationBarStyleOnDisappearing;
@property BOOL changesStatusBarStyle;
@property UIStatusBarStyle statusBarStyle;
@property BOOL restoresStatusBarStyleOnDisappearing;
// quickly sets up style changes that go together with the given bar style.
// it sets up the VC to restore older styles on disappearing, sets the navigation bar style/translucency/tint to match the constant passed, and if translucent also sets the VC to go fullscreen.
- (void) setChangesBarStyle:(UIStatusBarStyle) style;
// Returns the bundle instances of this class should search NIBs in. Default impl returns the main bundle.
+ (NSBundle*) nibBundle;
// convenience initializer; will call initWithNibName:bundle: with a nib name equal to the class name and the nib bundle returned by the class's +nibBundle method (the main bundle for the default implementation of that method).
- (id) init;
// creates a view controller hierarchy that can be presented modally. the returned view controller is not an instance of this class, but a wrapper that can be presented modally.
// the variable whose pointer is passed as argument, if that pointer is not NULL, will on return be set to the instance of this class that's contained in the view controller hierarchy just returned (autoreleased as per THE RULES).
// the default implementation creates a UINavigationController wrapping a new instance of the receiver class (produced via -init).
+ (UIViewController*) modalPaneForViewController:(id*) vc;
// dismisses this view controller from being modally presented, with animation. useful for UIBarButtonItems.
- (IBAction) dismiss;
@end
//
// ILViewController.m
// ILViewController
//
// Created by ∞ on 07/04/10.
//
// The contents of this file are in the public domain.
#import "ILViewController.h"
#if __IPHONE_3_2 <= __IPHONE_OS_VERSION_MAX_ALLOWED
#define ILViewControllerIsOniPad() (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#else
#define ILViewControllerIsOniPad() (NO)
#endif
@interface ILViewController ()
@property(retain) UIColor* oldNavigationBarTintColor;
@end
@implementation ILViewController
- (void) dealloc;
{
[oldNavigationBarTintColor release];
[navigationBarTintColor release];
[super dealloc];
}
#pragma mark Navbar/status bar appearance
- (void) viewWillAppear:(BOOL)animated;
{
[super viewWillAppear:animated];
if (self.changesNavigationBarStyle && self.navigationController) {
UINavigationBar* bar = self.navigationController.navigationBar;
if (self.restoresStatusBarStyleOnDisappearing) {
hasOlderNavigationBarStyle = YES;
oldNavigationBarStyle = bar.barStyle;
oldNavigationBarTranslucent = bar.translucent;
self.oldNavigationBarTintColor = bar.tintColor;
}
bar.barStyle = self.navigationBarStyle;
bar.translucent = self.navigationBarTranslucent;
if (bar.barStyle == UIBarStyleDefault)
bar.tintColor = self.navigationBarTintColor;
}
if (self.changesStatusBarStyle && !ILViewControllerIsOniPad()) {
if (restoresStatusBarStyleOnDisappearing) {
hasOlderStatusBarStyle = YES;
oldNavigationBarStyle = [[UIApplication sharedApplication] statusBarStyle];
}
[[UIApplication sharedApplication] setStatusBarStyle:self.statusBarStyle animated:animated];
}
}
- (void) viewWillDisappear:(BOOL)animated;
{
[super viewWillDisappear:animated];
if (self.restoresNavigationBarStyleOnDisappearing && hasOlderNavigationBarStyle) {
UINavigationBar* bar = self.navigationController.navigationBar;
bar.barStyle = oldNavigationBarStyle;
bar.translucent = oldNavigationBarTranslucent;
bar.tintColor = oldNavigationBarTintColor;
self.oldNavigationBarTintColor = nil;
hasOlderStatusBarStyle = NO;
}
if (self.restoresStatusBarStyleOnDisappearing && hasOlderStatusBarStyle && !ILViewControllerIsOniPad()) {
[[UIApplication sharedApplication] setStatusBarStyle:oldStatusBarStyle animated:animated];
hasOlderStatusBarStyle = NO;
}
}
@synthesize restoresNavigationBarStyleOnDisappearing, oldNavigationBarTintColor;
@synthesize restoresStatusBarStyleOnDisappearing;
@synthesize changesNavigationBarStyle;
@synthesize navigationBarStyle;
@synthesize navigationBarTintColor;
@synthesize navigationBarTranslucent;
@synthesize changesStatusBarStyle;
@synthesize statusBarStyle;
- (void) setChangesBarStyle:(UIStatusBarStyle)style;
{
self.changesStatusBarStyle = YES;
self.changesNavigationBarStyle = YES;
self.restoresNavigationBarStyleOnDisappearing = YES;
self.restoresStatusBarStyleOnDisappearing = YES;
self.statusBarStyle = style;
self.navigationBarTintColor = nil;
switch (style) {
case UIStatusBarStyleDefault:
self.navigationBarStyle = UIBarStyleDefault;
self.navigationBarTranslucent = NO;
break;
case UIStatusBarStyleBlackOpaque:
self.navigationBarStyle = UIBarStyleBlack;
self.navigationBarTranslucent = NO;
break;
case UIStatusBarStyleBlackTranslucent:
self.navigationBarStyle = UIBarStyleBlack;
self.navigationBarTranslucent = YES;
self.wantsFullScreenLayout = YES;
break;
default:
break;
}
}
#pragma mark Navigation item setup
- (void) setNavigationItem:(UINavigationItem *) ni;
{
self.navigationItem.title = ni.title;
self.navigationItem.prompt = ni.prompt;
self.navigationItem.backBarButtonItem = ni.backBarButtonItem;
self.navigationItem.titleView = ni.titleView;
self.navigationItem.hidesBackButton = ni.hidesBackButton;
self.navigationItem.leftBarButtonItem = ni.leftBarButtonItem;
self.navigationItem.rightBarButtonItem = ni.rightBarButtonItem;
}
- (UINavigationItem*) navigationItem;
{
return [super navigationItem]; // to silence compiler warnings
}
#pragma mark Common initialization
+ (NSBundle*) nibBundle;
{
return [NSBundle mainBundle];
}
- (id) init;
{
// we can't call -class on an object before we -init it, but we can access its isa pointer.
return [self initWithNibName:NSStringFromClass(self->isa) bundle:[self->isa nibBundle]];
}
#pragma mark Modal presentation
+ (UIViewController*) modalPaneForViewController:(id*) vc;
{
ILViewController* me = [[self alloc] init];
UINavigationController* nc = [[[UINavigationController alloc] initWithRootViewController:me] autorelease];
if (vc) *vc = me;
return nc;
}
- (IBAction) dismiss;
{
[self dismissModalViewControllerAnimated:YES];
}
#pragma mark Autorotation
@synthesize rotationStyle;
- (ILRotationStyle) rotationStyle;
{
if (rotationStyle == kILRotateIdiomDefault)
rotationStyle = ILViewControllerIsOniPad()? kILRotateAny : kILRotatePortrait;
return rotationStyle;
}
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) o;
{
switch (self.rotationStyle) {
case kILRotatePortrait:
return ILViewControllerIsOniPad()? UIInterfaceOrientationIsPortrait(o) : o == UIInterfaceOrientationPortrait;
case kILRotateLandscape:
return UIInterfaceOrientationIsLandscape(o);
case kILRotateAny:
return ILViewControllerIsOniPad()? YES : o != UIInterfaceOrientationPortraitUpsideDown;
default:
return NO;
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment