3

I have a ViewController with a NavigationController and I want to add a translucent UIView with some Buttons over the ViewController when I press a ViewController button, the problem is that I can not put the UIView over the NavigationBar. How can I solve this?

This is my code ( Very simple)

-(void)setOpacityView
{
    opacityVw = [[UIView alloc] initWithFrame:self.view.bounds];
    opacityVw.backgroundColor = [[UIColor alloc] initWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];


    WPCustomButton *closeBtn = [[WPCustomButton alloc] initWithFrame:CGRectMake(230, 10, 80, 20)];
    [closeBtn setTitle:@"Close X" forState:UIControlStateNormal];
    [closeBtn setBackgroundColor:[UIColor clearColor]];
    [closeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(closeView) forControlEvents:UIControlEventTouchUpInside];
    [opacityVw addSubview:closeBtn];
}

// ---------------------------------------------------------------------------------------------------------------------

#pragma mark - Button methods

-(void) closeView
{
    [opacityVw removeFromSuperview];
}


-(void)setProfileImage
{
    [self setOpacityView];

    [self.view addSubview:opacityVw];
}
croigsalvador
  • 1,993
  • 2
  • 24
  • 47

4 Answers4

4

I answered a similar question here

Try something like this:

-(void)setProfileImage
{
    [self setOpacityView];
    [self.navigationController.view addSubview:opacityVw];
}
Community
  • 1
  • 1
JonahGabriel
  • 3,066
  • 2
  • 18
  • 28
2

Add it to the AppDelegate's UIWindow instead.

- (void)setProfileImage
{
    [self setOpacityView];

    [ [[UIApplication sharedApplication] delegate].window addSubview:opacityVw];
}

Don't forget to change your view size:

opacityVw = [[UIView alloc] initWithFrame:[[[UIApplication sharedApplication] delegate]window].bounds];
Tim
  • 8,932
  • 4
  • 43
  • 64
0

Just make it simple :

-(void)setProfileImage
{
    [self setOpacityView];
    self.navigationController.navigationBarHidden = YES;
    [self.view insertSubview:opacityVw aboveSubview:self.view];
}

-(void) closeView
{
    [opacityVw removeFromSuperview];
    self.navigationController.navigationBarHidden = NO;
}
cdescours
  • 6,004
  • 3
  • 24
  • 30
0

You can create MainViewController and put that as your window.rootViewController. Add your navigationController to this MainViewController. After that it you add the view to your mainViewController, it would be on top of navigation Controller.

ManicMonkOnMac
  • 1,476
  • 13
  • 21