4

This is a fairly specific question so I'm not expecting a ton of responses, but it's worth a shot. I'm using SWRevealViewController for my app and I'm wondering if anyone has had any luck implementing a sliding status bar while using SWRevealViewController. I've had some luck but ran into issues when pushing new views.

If so, would you mind sharing how you were able to accomplish this?

Community
  • 1
  • 1
stewart715
  • 5,120
  • 8
  • 44
  • 78

2 Answers2

0

I think you could make use of the RevealControllerdelegate methods in order to animate or update the x position of it. Something like this:

Update Status Bar on manual sliding:

- (void)revealController:(SWRevealViewController *)revealController panGestureMovedToLocation:(CGFloat)location progress:(CGFloat)progress overProgress:(CGFloat)overProgress {
    NSLog(@"6progress: %f", progress);

    UIView *statusBarView = [[UIApplication sharedApplication] valueForKey:[@[@"status", @"Bar"] componentsJoinedByString:@""]];

    statusBarView.transform = CGAffineTransformMakeTranslation(progress * self.revealViewController.rearViewRevealWidth, 0.0f);

}

Update Status Bar on toggle action:

- (void) revealController:(SWRevealViewController *)revealController animateToPosition:(FrontViewPosition)position {

NSLog(@"animateToPosition: %ld", (long)position);

UIView *statusBarView = [[UIApplication sharedApplication] valueForKey:[@[@"status", @"Bar"] componentsJoinedByString:@""]];


if (!isDrawerOpen) {

    [UIView animateWithDuration:0.25 animations:^{
        statusBarView.transform = CGAffineTransformMakeTranslation(self.revealViewController.rearViewRevealWidth, 0.0f);
    } completion:^(BOOL finished) {
        isDrawerOpen = true;
    }];

} else {

    [UIView animateWithDuration:0.25 animations:^{
        statusBarView.transform = CGAffineTransformMakeTranslation(0, 0.0f);
    } completion:^(BOOL finished) {
        isDrawerOpen = false;
    }];

  }
}
Bista
  • 7,689
  • 3
  • 24
  • 52
Harol Higuera
  • 43
  • 1
  • 4
0

The above solution is not complete and does not work for all cases. Here the complete and robust solution.

1. Define local Boolean to set true when menu is open and false when menu is closed. @property (nonatomic, readwrite) BOOL isMenuOpen;. In my example, I synthesized it.

2. Detect when menu is open and when it is closed.

-(void)revealController:(SWRevealViewController *)revealController didMoveToPosition:(FrontViewPosition)position
{
    switch (position) {
        case FrontViewPositionLeftSideMostRemoved:
            NSLog(@"RevealView: FrontViewPositionLeftSideMostRemoved");
            break;

        case FrontViewPositionLeftSideMost:
            NSLog(@"RevealView: FrontViewPositionLeftSideMost");
            break;

        case FrontViewPositionLeftSide:
            NSLog(@"RevealView: FrontViewPositionLeftSide");
            break;

        case FrontViewPositionLeft:
            NSLog(@"RevealView: FrontViewPositionLeft");
            isMenuOpen = false;
            break;

        case FrontViewPositionRight:
            NSLog(@"RevealView: FrontViewPositionRight");
            isMenuOpen = true;
            break;

        case FrontViewPositionRightMost:
            NSLog(@"RevealView: FrontViewPositionRightMost");
            break;

        case FrontViewPositionRightMostRemoved:
            NSLog(@"RevealView: FrontViewPositionRightMostRemoved");
            break;

        default:
            break;
    }
}

3. Animate status bar similar to above answer, but more robust if e.g. menu is not closed entirely and snaps back open.

#pragma mark - SWRevealViewControllerDelegate

- (void)revealController:(SWRevealViewController *)revealController panGestureMovedToLocation:(CGFloat)location progress:(CGFloat)progress overProgress:(CGFloat)overProgress {
    UIView *statusBarView = [[UIApplication sharedApplication] valueForKey:[@[@"status", @"Bar"] componentsJoinedByString:@""]];

    statusBarView.transform = CGAffineTransformMakeTranslation(progress * self.revealViewController.rearViewRevealWidth, 0.0f);
}

- (void) revealController:(SWRevealViewController *)revealController animateToPosition:(FrontViewPosition)position {

    NSLog(@"animateToPosition: %ld", (long)position);

    UIView *statusBarView = [[UIApplication sharedApplication] valueForKey:[@[@"status", @"Bar"] componentsJoinedByString:@""]];


    if (position == FrontViewPositionRight) {
        [UIView animateWithDuration:0.25 animations:^{
            statusBarView.transform = CGAffineTransformMakeTranslation(self.revealViewController.rearViewRevealWidth, 0.0f);
        } completion:^(BOOL finished) {
            isMenuOpen = true;
        }];
    } else if (FrontViewPositionLeft) {
        [UIView animateWithDuration:0.25 animations:^{
            statusBarView.transform = CGAffineTransformMakeTranslation(0, 0.0f);
        } completion:^(BOOL finished) {
            isMenuOpen = false;
        }];
    }
}

4. Detect device rotation using notification. This is needed as the status bar is re-initialized when e.g. coming back from landscape to portrait. Put this in viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(deviceOrientationDidChange:)
                                             name:UIDeviceOrientationDidChangeNotification
                                           object:nil];

5. Handle device orientation change back to portrait (to be completed for upside-down).

- (void)deviceOrientationDidChange:(NSNotification *)notification
{
    [self updateOrientationForStatusBar];
}

- (void)updateOrientationForStatusBar
{
    switch ([UIDevice currentDevice].orientation)
    {
        case UIDeviceOrientationPortrait:
        {
            UIView *statusBarView = [[UIApplication sharedApplication] valueForKey:[@[@"status", @"Bar"] componentsJoinedByString:@""]];

            if (isMenuOpen) {
                statusBarView.transform = CGAffineTransformMakeTranslation(self.revealViewController.rearViewRevealWidth, 0.0f);
            } else {
                statusBarView.transform = CGAffineTransformMakeTranslation(0, 0.0f);
            }

        }
            break;

        case UIDeviceOrientationLandscapeLeft:
        case UIDeviceOrientationLandscapeRight:
        case UIDeviceOrientationPortraitUpsideDown:
        case UIDeviceOrientationUnknown:
        case UIDeviceOrientationFaceUp:
        case UIDeviceOrientationFaceDown:
            break;
    }
}
Herbert Bay
  • 216
  • 2
  • 14