1

I want to make a reddish translucent UINavigationBar (for iOS9), and I found that the following codes are essential:

    navController.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
    navController.navigationBar.shadowImage = UIImage()
    navController.navigationBar.backgroundColor = UIColor(red: 1.0,green: 0.3,blue: 0.3,alpha: 0.9)
    navController.navigationBar.translucent = true

It works fine if I simply created a UINavigationBar (instead of creating a UINavigationController), but it looks like this as I created a UINavigationController.

enter image description here I found the culprit to be the line navController.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default) , but if I remove it, the bar looks like this:

enter image description here So how can I solve this problem? (I'm creating the UINavigationController programmatically and sorry for the inconvenience that I cannot imbed images)

1 Answers1

0

Edit:

You need to subclass the UINavigationBar in order to get the translucent background of desired color. Have a look at this answer:

https://stackoverflow.com/a/19043174/2082569

Adding necessary code here too for reference:

@interface UnderlayNavigationBar : UINavigationBar

@end

@interface UnderlayNavigationBar ()
{
    UIView* _underlayView;
}

- (UIView*) underlayView;

@end

.

@implementation UnderlayNavigationBar

- (void) didAddSubview:(UIView *)subview
{
    [super didAddSubview:subview];

    if(subview != _underlayView)
    {
        UIView* underlayView = self.underlayView;
        [underlayView removeFromSuperview];
        [self insertSubview:underlayView atIndex:1];
    }
}

- (UIView*) underlayView
{
    if(_underlayView == nil)
    {
        const CGFloat statusBarHeight = 20;    //  Make this dynamic in your own code...
        const CGSize selfSize = self.frame.size;

        _underlayView = [[UIView alloc] initWithFrame:CGRectMake(0, -statusBarHeight, selfSize.width, selfSize.height + statusBarHeight)];
        [_underlayView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
        [_underlayView setBackgroundColor:[UIColor colorWithRed:1.0f green:0.3f blue:0.3f alpha:1.0f]];
        [_underlayView setAlpha:0.7f];
        [_underlayView setUserInteractionEnabled:NO];
    }

    return _underlayView;
}

@end

.

UIViewController* rootViewController = ...;
UINavigationController* navigationController = [[UINavigationController alloc] initWithNavigationBarClass:[UnderlayNavigationBar class] toolbarClass:nil];
[navigationController setViewControllers:@[rootViewController]];
Community
  • 1
  • 1
atulkhatri
  • 10,896
  • 3
  • 53
  • 89
  • It still doesn't work for me.. It works rightly on your device? – user2232335 Apr 25 '16 at 02:36
  • Sorry, I was using a custom navigation bar in my project. Took the relevant portion from an existing answer on SO. Edited the answer; Please take a look and let me know if it works. Of course the answer is in Objective-C so you might need to convert this to swift. :) – atulkhatri Apr 25 '16 at 05:30