Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the method "sendSubviewToBack" appropriately?

I have implemented the slide menu but I want sliding effect exactly like facebook. I came across following post on stackoverflow:

iphone facebook side menu using objective c

In which I want to implement the solution given by greenisus (up voted 15 times) but I am having the trouble implementing that, same trouble has been raised by Bot in comments to his answer. "@greenisus I'm confused on how you sent the menu to the back? When I do that it just shows a black side menu." which has not been answered.

The text of his answer for reference is:

It's pretty simple really. First, you need to make a view controller that sits under the one that's visible. You can send that view to the back like this:

[self.view sendSubviewToBack:menuViewController.view];

Then, you put a menu button on the left side of your navigation bar, and write a handler kind of like this:

- (void)menuButtonPressed:(id)sender {
    CGRect destination = self.navigationController.view.frame;
    if (destination.origin.x > 0) {
        destination.origin.x = 0;
    } else {
        destination.origin.x += 254.5;
    }
    [UIView animateWithDuration:0.25 animations:^{
        self.navigationController.view.frame = destination;        
    } completion:^(BOOL finished) {
        self.view.userInteractionEnabled = !(destination.origin.x > 0);
    }];
}

That's the general idea. You may have to change the code to reflect your view hierarchy, etc.

Just want to know when we have to use the following method, the second method is simple and works fine, but doesn't show menu view under it.

[self.view sendSubviewToBack:menuViewController.view];

Looking for some pointers or solution to run the above code correctly.

like image 270
mangesh Avatar asked Aug 18 '12 10:08

mangesh


1 Answers

Actually you should know how can we do it. just add the menuViewController.view as the subView of self.view. But this will cover the navigationController.view, so you can just [self.view sendSubviewToBack:menuViewController.view]. And when you need to show/hide the menuViewController you need to use the method - (void)menuButtonPressed:(id)sender.

In a HomeViewController:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // be careful with the sequence of the code. If you firstly add the contentViewController and then add the 
    // menuViewController you need to [self.view sendSubviewToBack:self.menuViewController.view], else you don't
    // need to use sendSubviewToBack.
    self.menuViewController = [[MenuViewController alloc] init];
    self.contentViewController = [[ContentViewController alloc] init];
    [self.view addSubview:self.menuViewController.view];
    [self.view addSubview:self.contentViewController.view];
}

In ContentViewController:

- (IBAction)showMenu:(id)sender
{
    CGRect destination = self.view.frame;
    if (destination.origin.x > 0) {
        destination.origin.x = 0;
    } else {
        destination.origin.x += 254.5;
    }
    [UIView animateWithDuration:0.25 animations:^{
        self.view.frame = destination;        
    } completion:^(BOOL finished) {
        //self.view.userInteractionEnabled = !(destination.origin.x > 0);
    }];
}
like image 122
sunkehappy Avatar answered Nov 19 '22 18:11

sunkehappy