Skip to content

Instantly share code, notes, and snippets.

@jlcampana
Created September 18, 2012 12:51
Show Gist options
  • Save jlcampana/3742924 to your computer and use it in GitHub Desktop.
Save jlcampana/3742924 to your computer and use it in GitHub Desktop.
Emulate UINavigationController with views
#import "NSMutableArray+Stack.h"
#define SHOW_ANIMATION_DURATON 0.5f
@property (nonatomic, strong) NSMutableArray *viewStack;
-(void)pushView:(UIView *)view
{
UIView *anterior = [self.viewStack peek];
[self.viewStack push:view];
UIView *fatherView = self.view;
CGRect f = fatherView.bounds;
f.origin.x = f.size.width;
[fatherView addSubview:view];
[view setFrame:f];
[fatherView bringSubviewToFront:view];
CGRect f2 = anterior.frame;
f2.origin.x = -f2.size.width;
f.origin.x = 0.0f;
UINavigationItem *navigation = self.navigationItem;
//Animation
[UIView animateWithDuration:SHOW_ANIMATION_DURATON delay:0.0f options:UIViewAnimationOptionAllowUserInteraction
animations:^{
[view setFrame:f];
[anterior setFrame:f2];
}
completion:^(BOOL finished){
[anterior setHidden:YES];
if([self.viewStack count]==2)
{
UIBarButtonItem *izq;
izq = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_back"] title:@"Back" target:self action:@selector(popView)];
[navigation setLeftBarButtonItem:izq];
}
}
];
}
-(void)popView
{
if ([self.viewStack count]>1)
{
UIView *view = [self.viewStack pop];
UIView *anterior = [self.viewStack peek];
UIView *fatherView = self.view;
CGRect f = fatherView.bounds;
f.origin.x = -f.size.width;
[anterior setHidden:NO];
[anterior setFrame:f];
f.origin.x = 0;
CGRect f2 = f;
f2.origin.x = f2.size.width;
//Animation
[UIView animateWithDuration:SHOW_ANIMATION_DURATON delay:0.0f options:UIViewAnimationOptionAllowUserInteraction
animations:^{
[anterior setFrame:f];
[view setFrame:f2];
}
completion:^(BOOL finished){
[view removeFromSuperview];
if ([self.viewStack count]==1)
{
self.navigationItem.leftBarButtonItem = nil;
}
}
];
}
else
{
self.navigationItem.leftBarButtonItem = nil;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment