How to display a UIView which overlaps the UINavigationBar

488 Views Asked by At

In my application, I want to initiate a UIView that overlaps and dims the whole screen including the UINavigationBar, the code is as below:

- (void)showInstruction
{
    self.holedView = [[JMHoledView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:self.holedView];
}

but indeed the self.holedView can only dims the district without the UINavigationBar on the screen. How can I do that?

4

There are 4 best solutions below

1
On BEST ANSWER

You can add view as a subview to the window's or navigation controller's view.

[self.navigationController.view addSubview:yourView];

OR

[[[[UIApplication sharedApplication] delegate] window] addSubview:yourView];

Create view's delegate to remove it from superview whenever needed

0
On

You can use dimmed transparent image and set it as background image of navigation bar.

For eg In my case , I made black transparent image of alpha 0.2 and set it as navigation background image and made the background color clear color

 [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"black_patch.png"]
                                              forBarMetrics:UIBarMetricsDefault];
[UINavigationBar appearance].translucent = YES;
navController.view.backgroundColor = [UIColor clearColor];
0
On

I use this to add subView in the main window:

[[UIApplication sharedApplication].keyWindow addSubview:view];
0
On

you can add it to the window

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication  sharedApplication] delegate];
UIView *backgrView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)];
backgrView.backgroundColor = [UIColor blackColor];
backgrView.alpha = 0.6;
[[appDelegate window] addSubview:backgrView];

or

Try navigationBar.translucent = NO; , It is YES by default in iOS7.


 So you can add a version check like this:

 float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
if (systemVersion >= 7.0)
{
 navigationBar.translucent = NO;
}