Skip to content

Instantly share code, notes, and snippets.

@adamgall
Last active August 29, 2015 13:57
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 adamgall/9807941 to your computer and use it in GitHub Desktop.
Save adamgall/9807941 to your computer and use it in GitHub Desktop.
// NINavigationBar.h
@protocol ScheduleNavigationDelegate
- (NSString *)getScheduleScreenIdentifier;
@end
@interface NINavigationBarSchedule : UINavigationBar
@property (nonatomic, strong) id<ScheduleNavigationDelegate> scheduleDelegate;
@end
// NINavigationBar.m
@interface NINavigationBarSchedule ()
@property (nonatomic, strong) NIScheduleNavigationDropdown *dropDown;
@end
@implementation NINavigationBarSchedule
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self addNavigationSlideDownGesture];
[self makeSlideDownView];
}
return self;
}
- (void)addNavigationSlideDownGesture
{
UITapGestureRecognizer *singleTap;
singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTap:)];
[singleTap setNumberOfTouchesRequired:1];
[singleTap setEnabled:YES];
[self addGestureRecognizer:singleTap];
}
- (void)makeSlideDownView
{
CGRect frame = CGRectMake(0, 0, 320, 144);
frame.origin.y = CGRectGetMaxY(self.frame) - frame.size.height + 20;
self.dropDown = [[NIScheduleNavigationDropdown alloc] initWithFrame:frame];
}
- (void)didTap:(id)sender
{
// find the subclassed UINavigationItem that has all the custom styles applied to it
NINavigationItemSchedule *scheduleItem = nil;
for (UINavigationItem *item in self.items) {
if ([item isKindOfClass:[NINavigationItemSchedule class]]) {
scheduleItem = (NINavigationItemSchedule *)item;
break;
}
}
// show or hide the dropdown
CGFloat slideTime = 0.25f;
if (self.dropDown.hidden) {
[self slideNavigationMenuDownWithDuration:slideTime forNavigationItem:scheduleItem];
} else {
[self slideNavigationMenuUpWithDuration:slideTime forNavigationItem:scheduleItem];
}
}
- (void)slideNavigationMenuDownWithDuration:(CGFloat)duration forNavigationItem:(NINavigationItemSchedule *)scheduleItem
{
// flip the chevron arrow
scheduleItem.titleView = [scheduleItem upChevronAttributedLabel];
self.dropDown.hidden = NO;
[self.superview insertSubview:self.dropDown belowSubview:self];
[self.dropDown setHighlightedItem:[self.scheduleDelegate getScheduleScreenIdentifier]];
[UIView animateWithDuration:duration animations:^{
CGRect frame = self.dropDown.frame;
frame.origin.y = CGRectGetMaxY(self.frame);
self.dropDown.frame = frame;
}];
}
- (void)slideNavigationMenuUpWithDuration:(CGFloat)duration forNavigationItem:(NINavigationItemSchedule *)scheduleItem
{
// flip the chevron arrow
scheduleItem.titleView = [scheduleItem downChevronAttributedLabel];
[UIView animateWithDuration:duration animations:^{
CGRect frame = CGRectMake(0, 0, 320, 144);
frame.origin.y = CGRectGetMaxY(self.frame) - frame.size.height;
self.dropDown.frame = frame;
} completion:^(BOOL finished) {
self.dropDown.hidden = YES;
}];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment