How to use sendSubviewToBack method correctly?

I have implemented a slide menu, but I need a sliding effect just like on facebook. I came across the following post in stackoverflow:

iphone facebook side menu using target c

That I want to implement the solution given by greenisus (voted 15 times), but I had a problem with the implementation of this, the same concern was raised by Bot in the comments to his answer. "@greenisus I'm confused about how you sent the menu to the back? When I do this, it just shows the menu from the black side." to which I did not answer.

The text of his answer for reference:

It is pretty simple. First, you need to create a view controller that is below the visible. You can put this view in the background as follows:

[self.view sendSubviewToBack:menuViewController.view]; 

Then you place the menu button on the left side of the navigation panel and record the view of the handler as follows:

 - (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); }]; } 

This is a general idea. You may need to modify the code to reflect the hierarchy of your view, etc.

I just want to know when we should use the following method, the second method is simple and works fine, but does not display it below it.

 [self.view sendSubviewToBack:menuViewController.view]; 

Look for some pointers or a solution for the proper execution of the above code.

+8
ios objective-c iphone uiviewcontroller uiview
source share
1 answer

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

In 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); }]; } 
+2
source share

All Articles