Julie Yu
Julie Yu

Reputation: 83

How to display a UIView which overlaps the UINavigationBar

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?

Upvotes: 0

Views: 488

Answers (4)

Shanmugasundharam
Shanmugasundharam

Reputation: 2092

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;
}

Upvotes: 0

Ashwani
Ashwani

Reputation: 388

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

Upvotes: 2

Hossam Ghareeb
Hossam Ghareeb

Reputation: 7113

I use this to add subView in the main window:

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

Upvotes: 0

Mukesh
Mukesh

Reputation: 3690

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];

Upvotes: 1

Related Questions