Last active
December 17, 2015 09:29
-
-
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).
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
#import <Foundation/Foundation.h> | |
@interface DualNibViewController : UIViewController { | |
NSDictionary *_viewStates; | |
} | |
@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
#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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment