Skip to content

Commit 17c29fc

Browse files
committed
- Making the methods for opening and closing menu public
- Added 2 new public methods to toggle left and right menu, to be used for customization (Passing a custom UIBArButtonItem with a UIButton as custom view) - Added a public method to determine whether the menu is open or not - Fixed the issue where on iOS6 landscape mode the menu would go below the status bar - Fixed the problem fade animation was applying alpha to the menu instead of fadeView - Added all content to iPad story board for demo
1 parent 534113d commit 17c29fc

File tree

7 files changed

+873
-264
lines changed

7 files changed

+873
-264
lines changed

SlideMenu/AppDelegate.m

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,23 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
1717

1818
MenuViewController *rightMenu = (MenuViewController*)[mainStoryboard
1919
instantiateViewControllerWithIdentifier: @"MenuViewController"];
20-
rightMenu.view.backgroundColor = [UIColor whiteColor];
2120
rightMenu.cellIdentifier = @"rightMenuCell";
2221

2322
MenuViewController *leftMenu = (MenuViewController*)[mainStoryboard
2423
instantiateViewControllerWithIdentifier: @"MenuViewController"];
25-
leftMenu.view.backgroundColor = [UIColor whiteColor];
2624
leftMenu.cellIdentifier = @"leftMenuCell";
2725

2826
[SlideNavigationController sharedInstance].rightMenu = rightMenu;
2927
[SlideNavigationController sharedInstance].leftMenu = leftMenu;
3028

29+
// Creating a custom bar button for right menu
30+
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
31+
[button setImage:[UIImage imageNamed:@"menu-button"] forState:UIControlStateNormal];
32+
[button addTarget:[SlideNavigationController sharedInstance] action:@selector(toggleRightMenu) forControlEvents:UIControlEventTouchUpInside];
33+
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
34+
[SlideNavigationController sharedInstance].rightBarButtonItem = rightBarButtonItem;
35+
36+
3137
// Override point for customization after application launch.
3238
return YES;
3339
}

SlideMenu/Helper Classes/FriendsViewController.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111
@implementation FriendsViewController
1212

13+
- (void)viewDidLoad
14+
{
15+
[super viewDidLoad];
16+
}
1317

1418
- (BOOL)slideNavigationControllerShouldDisplayLeftMenu
1519
{
@@ -21,4 +25,16 @@ - (BOOL)slideNavigationControllerShouldDisplayRightMenu
2125
return NO;
2226
}
2327

28+
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
29+
{
30+
return 20;
31+
}
32+
33+
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
34+
{
35+
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"friendCell"];
36+
cell.textLabel.text = [NSString stringWithFormat:@"Friend %d", indexPath.row];
37+
return cell;
38+
}
39+
2440
@end

SlideMenu/Helper Classes/MenuViewController.m

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ @implementation MenuViewController
1515

1616
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
1717
{
18-
return 4;
18+
return 15;
1919
}
2020

2121
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
@@ -39,6 +39,10 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
3939
case 3:
4040
cell.textLabel.text = @"Sign Out";
4141
break;
42+
43+
default:
44+
cell.textLabel.text = @"Random Cell";
45+
break;
4246
}
4347

4448
return cell;
@@ -69,6 +73,9 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
6973
[[SlideNavigationController sharedInstance] popToRootViewControllerAnimated:YES];
7074
return;
7175
break;
76+
77+
default:
78+
return;
7279
}
7380

7481
[[SlideNavigationController sharedInstance] switchToViewController:vc withCompletion:nil];

SlideMenu/Source/SlideNavigationController.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,10 @@ typedef enum{
6363

6464
+ (SlideNavigationController *)sharedInstance;
6565
- (void)switchToViewController:(UIViewController *)viewController withCompletion:(void (^)())completion;
66+
- (void)openMenu:(Menu)menu withCompletion:(void (^)())completion;
67+
- (void)closeMenuWithCompletion:(void (^)())completion;
68+
- (void)toggleLeftMenu;
69+
- (void)toggleRightMenu;
70+
- (BOOL)isMenuOpen;
6671

6772
@end

SlideMenu/Source/SlideNavigationController.m

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ @interface SlideNavigationController()
3636

3737
@implementation SlideNavigationController
3838

39+
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
3940
#define MENU_SLIDE_ANIMATION_DURATION .3
4041
#define MENU_QUICK_SLIDE_ANIMATION_DURATION .18
4142
#define MENU_IMAGE @"menu-button"
@@ -44,7 +45,8 @@ @implementation SlideNavigationController
4445
#define MENU_DEFAULT_SLIDE_OFFSET 60
4546
#define MENU_FAST_VELOCITY_FOR_SWIPE_FOLLOW_DIRECTION 1200
4647
#define MENU_REVEAL_ANIMATION_DEFAULT_SLIDE_MOVEMENT 100
47-
#define MENU_REVEAL_ANIMATION_DEFAULT_FADE_MAXIMUM_ALPHA .9
48+
#define MENU_REVEAL_ANIMATION_DEFAULT_FADE_MAXIMUM_ALPHA .7
49+
#define STATUS_BAR_HEIGHT 20
4850

4951
static SlideNavigationController *singletonInstance;
5052

@@ -131,6 +133,11 @@ - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceO
131133
CGRect rect = self.view.frame;
132134
self.leftMenu.view.frame = rect;
133135
self.rightMenu.view.frame = rect;
136+
137+
// Move menus accordingly to avoid a weird animation during opening/closing menu after a rotation
138+
[self updateMenuAnimation:MenuLeft];
139+
[self updateMenuAnimation:MenuRight];
140+
134141
self.view.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.view.bounds].CGPath;
135142

136143
// we set shadowOpacity to 0 in willRotateToInterfaceOrientation, after the rotation we want to add the shadow back
@@ -177,6 +184,33 @@ - (void)switchToViewController:(UIViewController *)viewController withCompletion
177184
}
178185
}
179186

187+
- (void)closeMenuWithCompletion:(void (^)())completion
188+
{
189+
[self closeMenuWithDuration:MENU_SLIDE_ANIMATION_DURATION andCompletion:completion];
190+
}
191+
192+
- (void)openMenu:(Menu)menu withCompletion:(void (^)())completion
193+
{
194+
[self openMenu:menu withDuration:MENU_SLIDE_ANIMATION_DURATION andCompletion:completion];
195+
}
196+
197+
- (void)toggleLeftMenu
198+
{
199+
[self toggleMenu:MenuLeft withCompletion:nil];
200+
}
201+
202+
- (void)toggleRightMenu
203+
{
204+
[self toggleMenu:MenuRight withCompletion:nil];
205+
}
206+
207+
- (BOOL)isMenuOpen
208+
{
209+
return (self.horizontalLocation == 0) ? NO : YES;
210+
}
211+
212+
#pragma mark - Override Methods -
213+
180214
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated
181215
{
182216
if ([self isMenuOpen])
@@ -225,6 +259,14 @@ - (NSArray *)popToViewController:(UIViewController *)viewController animated:(BO
225259

226260
#pragma mark - Private Methods -
227261

262+
- (void)toggleMenu:(Menu)menu withCompletion:(void (^)())completion
263+
{
264+
if ([self isMenuOpen])
265+
[self closeMenuWithCompletion:completion];
266+
else
267+
[self openMenu:menu withCompletion:completion];
268+
}
269+
228270
- (UIBarButtonItem *)barButtonItemForMenu:(Menu)menu
229271
{
230272
SEL selector = (menu == MenuLeft) ? @selector(leftMenuSelected:) : @selector(righttMenuSelected:);
@@ -243,11 +285,6 @@ - (UIBarButtonItem *)barButtonItemForMenu:(Menu)menu
243285
}
244286
}
245287

246-
- (BOOL)isMenuOpen
247-
{
248-
return (self.horizontalLocation == 0) ? NO : YES;
249-
}
250-
251288
- (BOOL)shouldDisplayMenu:(Menu)menu forViewController:(UIViewController *)vc
252289
{
253290
if (menu == MenuRight)
@@ -291,11 +328,6 @@ - (void)openMenu:(Menu)menu withDuration:(float)duration andCompletion:(void (^)
291328
}];
292329
}
293330

294-
- (void)openMenu:(Menu)menu withCompletion:(void (^)())completion
295-
{
296-
[self openMenu:menu withDuration:MENU_SLIDE_ANIMATION_DURATION andCompletion:completion];
297-
}
298-
299331
- (void)closeMenuWithDuration:(float)duration andCompletion:(void (^)())completion
300332
{
301333
[self.topViewController.view removeGestureRecognizer:self.tapRecognizer];
@@ -314,11 +346,6 @@ - (void)closeMenuWithDuration:(float)duration andCompletion:(void (^)())completi
314346
}];
315347
}
316348

317-
- (void)closeMenuWithCompletion:(void (^)())completion
318-
{
319-
[self closeMenuWithDuration:MENU_SLIDE_ANIMATION_DURATION andCompletion:completion];
320-
}
321-
322349
- (void)moveHorizontallyToLocation:(CGFloat)location
323350
{
324351
CGRect rect = self.view.frame;
@@ -353,6 +380,7 @@ - (void)updateMenuAnimation:(Menu)menu
353380

354381
if (self.menuRevealAnimation == MenuRevealAnimationFade || self.menuRevealAnimation == MenuRevealAnimationSlideAndFade)
355382
{
383+
self.menuRevealFadeAnimationView.frame = menuViewController.view.bounds;
356384
[menuViewController.view addSubview:self.menuRevealFadeAnimationView];
357385
self.menuRevealFadeAnimationView.alpha = self.menuRevealAnimationFadeMaximumAlpha - (self.menuRevealAnimationFadeMaximumAlpha *progress);
358386
}
@@ -370,14 +398,29 @@ - (void)updateMenuAnimation:(Menu)menu
370398
location = (location < 0) ? 0 : location;
371399

372400
CGRect rect = menuViewController.view.frame;
401+
BOOL isIos7 = SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0");
373402

374403
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
375404
{
376405
rect.origin.y = (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight) ? location : location*-1;
406+
407+
if (!isIos7)
408+
{
409+
// For some reasons in landscape belos the status bar is considered y=0, but in portrait it's considered y=20
410+
rect.origin.x = (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight) ? 0 : STATUS_BAR_HEIGHT;
411+
rect.size.width = self.view.frame.size.width-STATUS_BAR_HEIGHT;
412+
}
377413
}
378414
else
379415
{
380416
rect.origin.x = (self.interfaceOrientation == UIInterfaceOrientationPortrait) ? location : location*-1;
417+
418+
if (!isIos7)
419+
{
420+
// For some reasons in landscape belos the status bar is considered y=0, but in portrait it's considered y=20
421+
rect.origin.y = (self.interfaceOrientation == UIInterfaceOrientationPortrait) ? STATUS_BAR_HEIGHT : 0;
422+
rect.size.height = self.view.frame.size.height-STATUS_BAR_HEIGHT;
423+
}
381424
}
382425

383426
menuViewController.view.frame = rect;
@@ -395,7 +438,7 @@ - (void)prepareMenuForReveal:(Menu)menu
395438

396439
if (self.menuRevealAnimation == MenuRevealAnimationFade || self.menuRevealAnimation == MenuRevealAnimationSlideAndFade)
397440
{
398-
menuViewController.view.alpha = self.menuRevealAnimationFadeMaximumAlpha;
441+
self.menuRevealFadeAnimationView.alpha = self.menuRevealAnimationFadeMaximumAlpha;
399442
self.menuRevealFadeAnimationView.frame = menuViewController.view.bounds;
400443
}
401444

@@ -644,7 +687,6 @@ - (UIView *)menuRevealFadeAnimationView
644687
if (!_menuRevealFadeAnimationView)
645688
{
646689
_menuRevealFadeAnimationView = [[UIView alloc] init];
647-
_menuRevealFadeAnimationView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
648690
_menuRevealFadeAnimationView.backgroundColor = [UIColor blackColor];
649691
_menuRevealFadeAnimationView.frame = self.view.bounds;
650692
}

0 commit comments

Comments
 (0)