Skip to content

Instantly share code, notes, and snippets.

@andreyvit
Last active December 17, 2015 09:29
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 andreyvit/5587836 to your computer and use it in GitHub Desktop.
Save andreyvit/5587836 to your computer and use it in GitHub Desktop.
An old code fragment demonstrating the use of a separate landscape .nib in a UIViewController (the view hierarchies of the two .nib files must match exactly, and only the frames are adjusted when going into landscape, although you can obviously add more properties to the save/restore code).
#import <Foundation/Foundation.h>
@interface DualNibViewController : UIViewController {
NSDictionary *_viewStates;
}
@end
#import "DualNibViewController.h"
@implementation DualNibViewController
#define kViewStatePortrait @"Portrait"
#define kViewStateLandscape @"Landscape"
+ (NSString *)storedStateForView:(UIView *)view {
NSLog(@"Storing state %@ of view %@.", NSStringFromCGRect(view.frame), NSStringFromClass([view class]));
return NSStringFromCGRect(view.frame);
}
+ (void)restoreState:(NSString *)state forView:(UIView *)view {
NSLog(@"Restoring state %@ to view %@.", state, NSStringFromClass([view class]));
view.frame = CGRectFromString(state);
}
- (void)saveViewStatesInViewHierarchyOfView:(UIView *)view withPath:(NSString *)path into:(NSMutableDictionary *)dictionary {
[dictionary setObject:[DualNibViewController storedStateForView:view] forKey:path];
NSInteger index = 0;
for (UIView *subview in view.subviews) {
[self saveViewStatesInViewHierarchyOfView:subview
withPath:[path stringByAppendingFormat:@".%d", index]
into:dictionary];
++index;
}
}
- (void)restoreViewStatesInViewHierarchyOfView:(UIView *)view withPath:(NSString *)path from:(NSDictionary *)dictionary {
[DualNibViewController restoreState:[dictionary objectForKey:path] forView:view];
NSInteger index = 0;
for (UIView *subview in view.subviews) {
[self restoreViewStatesInViewHierarchyOfView:subview
withPath:[path stringByAppendingFormat:@".%d", index]
from:dictionary];
++index;
}
}
- (void)saveViewStates {
NSString *landscapeNibName = [self.nibName stringByAppendingString:@"_Landscape"];
NSMutableDictionary *landscapeViews = [[[NSMutableDictionary alloc] init] autorelease];
[[NSBundle mainBundle] loadNibNamed:landscapeNibName owner:landscapeViews options:nil];
NSMutableDictionary *portraitStates = [[[NSMutableDictionary alloc] init] autorelease];
NSMutableDictionary *landscapeStates = [[[NSMutableDictionary alloc] init] autorelease];
UIView *landscapeView = [landscapeViews objectForKey:@"view"];
// handle outlets
// for (NSString *outletName in [landscapeViews allKeys]) {
// if ([outletName isEqualToString:@"view"])
// continue;
// UIView *portraitView = [self valueForKey:outletName];
// UIView *landscapeView = [landscapeViews objectForKey:outletName];
// [portraitStates setObject:[DualNibViewController storedStateForView:portraitView] forKey:outletName];
// [landscapeStates setObject:[DualNibViewController storedStateForView:landscapeView] forKey:outletName];
// }
[self saveViewStatesInViewHierarchyOfView:self.view withPath:@"view" into:portraitStates];
[self saveViewStatesInViewHierarchyOfView:landscapeView withPath:@"view" into:landscapeStates];
[_viewStates release];
_viewStates = [[NSDictionary dictionaryWithObjectsAndKeys:[NSDictionary dictionaryWithDictionary:portraitStates], kViewStatePortrait,
[NSDictionary dictionaryWithDictionary:landscapeStates], kViewStateLandscape,
nil] retain];
}
- (void)restoreViewStatesForInterfaceOrientation:(UIInterfaceOrientation)orientation {
NSString *orientationName = UIInterfaceOrientationIsLandscape(orientation) ? kViewStateLandscape : kViewStatePortrait;
NSDictionary *state = [_viewStates objectForKey:orientationName];
// for (NSString *outlet in [state allKeys]) {
// UIView *view = [self valueForKey:outlet];
// [DualNibViewController restoreState:[state objectForKey:outlet] forView:view];
// }
[self restoreViewStatesInViewHierarchyOfView:self.view withPath:@"view" from:state];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (_viewStates == nil)
[self saveViewStates];
[self restoreViewStatesForInterfaceOrientation:[UIDevice currentDevice].orientation];
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[self restoreViewStatesForInterfaceOrientation:toInterfaceOrientation];
}
- (void)dealloc {
[_viewStates release];
[super dealloc];
}
@end
#import <UIKit/UIKit.h>
#import "DualNibViewController.h"
@interface PlayMenuViewController : DualNibViewController {
IBOutlet UIButton *playXxxButton;
IBOutlet UIButton *playYyyButton;
IBOutlet UIButton *playZzzButton;
IBOutlet UIButton *playQqqButton;
}
@end
#import "PlayMenuViewController.h"
@implementation PlayMenuViewController
- (id)init {
if (self = [super initWithNibName:NSStringFromClass([self class]) bundle:nil]) {
}
return self;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment