Search code examples
objective-ciossidebar

SideMenu black view


I am trying to make a sidebar menu but i have a little problem.

I explain :

  1. I created an UIViewController that i called sideMenuViewController
  2. In my viewController Class (the initial view controller), in the header file, i import my class SideMenuViewController and i wrote :

    -(IBAction)openSideMenu:(id)sender;

    @property(nonatomic, retain) SideMenuViewController *sideMenu;
    

The openSideMenu action is associated to the menu button.Menu button

I implemented this method like this :

- (IBAction)openSideMenu:(id)sender {
    CGRect destination = self.view.frame;

    if(destination.origin.x > 0){
        destination.origin.x = 0;
    }else{
        destination.origin.x += SideMenuX;
    }

    [UIView animateWithDuration:0.4 animations:^{
        self.view.frame = destination;
    }completion:^(BOOL finished) {
        if(finished){

        }
    }];
}

SideMenuX is a macro : #define SideMenuX 154.4

My viewDidLoad method looks like this :

- (void)viewDidLoad
{
    [super viewDidLoad];
    _sideMenu = [[SideMenuViewController alloc] init];
    [self.view sendSubviewToBack:_sideMenu.view];
    // Do any additional setup after loading the view, typically from a nib.
}

The problem is that when i click on the menu button, i get a black screen and not my side menu view.

Black screen side Bar Menu

Thank you in advance !


Solution

  • Two problems:

    1. You are not adding the sideMenu at all. Try adding it to the parent view (self.view.superview), which in your case most likely will be the UIWindow:
       [self.view.superview insertSubview:_sideMenu.view belowSubview:self.view];
      If you are using a navigation controller, use self.navigationController.view instead self.view.
    2. Not sure if you initialized the view with a NIB or the Storyboard (see below if you didn't).

    Here is a working example. I created the left view controller inside the storyboard like this:

    • Throw a View Controller component on the storyboard.
    • Select the controller on the left column, and go to the Identity Inspector on the right column (alt+cmd+3):
      • Set the Class to SideMenuViewController
      • Set the Storyboard ID to SideMenuViewController

    Instantiate the controller inside viewDidLoad with

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    self.sideMenu = (SideMenuViewController*)[storyboard instantiateViewControllerWithIdentifier:@"SideMenuViewController"];
    

    then insert it as child of the superview.


    (Answering the comment below)

    This line is the problem:

    [self.view.superview addSubview:_sideMenu.view];
    

    In a NIB based project the superview is UIWindow, but in a Storyboard project, the self.view.superview of a UIViewController is nil. You can solve this, for example, adding a UINavigationViewController. Follow these steps:

    • Throw in a "Navigation Controller"
    • Delete the view controller it points to.
    • Press Ctrl and drag the pointer from the UINavigationController to your view controller, and select "root view controller" on the dialog that appears.
    • Drag the arrow pointing to your view controller to the UINavigationController (the one that marks the initial view controller, not the one that comes from UINavigationController).

    Then change your code to

    _sideMenu = [[SideMenuViewController alloc] initWithNibName:@"SideMenuViewController" bundle:nil];
    [self.navigationController.view.superview insertSubview:_sideMenu.view belowSubview:self.navigationController.view];
    

    To hide the navigation bar of the UINavigationController, select it in the Storyboard and click Hidden in the Attributes Inspector (alt+cmd+4).