Get the Right Color in iOS7 Translucent Navigation Bar

Get the right color in iOS7 translucent navigation bar

The bar will adjust your color values.

Preferred method, for RGB >= 40 only, will give the most blurring

You can use this calculator and put in what you want the color to be when rendered on screen, it will tell you what to set the color of the barTintColor so when Apple adjusts it, it will show as intended

https://www.transpire.com/insights/blog/bar-color-calculator/

Edit: Note that these calculations are for a white background, and for lighter colours (rgb over 40, if you need darker, you will need to add a background layer like others have mentioned - although that will reduce the bar's blur)

In depth guide: https://www.transpire.com/insights/blog/custom-ui-navigationbar-colors-ios7/

Snippet:

@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:0.0f green:0.34f blue:0.62f alpha:1.0f]];
[_underlayView setAlpha:0.36f];
[_underlayView setUserInteractionEnabled:NO];
}

return _underlayView;
}

@end

.

UIViewController* rootViewController = ...;
UINavigationController* navigationController = [[UINavigationController alloc] initWithNavigationBarClass:[UnderlayNavigationBar class] toolbarClass:nil];
[navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:0.0f green:0.0f blue:90.0f/255.0f alpha:1]];
[navigationController setViewControllers:@[rootViewController]];

How can I get the color and translucency of the iOS 10 watch Navigation Bar?

Use below method to achieve navigationBar translucent effect. When UIContentView scrolls as the picture you posted.

In viewDidLoad:

  view.backgroundColor = UIColor.black
navigationController?.navigationBar.barStyle = UIBarStyle.blackTranslucent
navigationController?.navigationBar.tintColor = UIColor.white

Updated

Sample Image

Output:

Sample Image

Note:

  • Screen recording is not showing the real result.I recommend you to test the code in a real device.

  • You can use the same method. If you developing tabBar based project.

Achieving bright, vivid colors for an iOS 7 translucent UINavigationBar

iOS 7.0.3 UPDATE: As you see above 7.0.3 changed things. I've updated my gist. Hopefully this will just go away as people upgrade.

Original Answer:
I ended up with a hack combining the two of the other answers. I'm subclassing UINavigationBar and adding a layer to the back with some extra space to cover if any of the various height status bars are up. The layer gets adjusted in layout subviews and the color changes whenever you set barTintColor.

Gist: https://gist.github.com/aprato/6631390

setBarTintColor

  [super setBarTintColor:barTintColor];
if (self.extraColorLayer == nil) {
self.extraColorLayer = [CALayer layer];
self.extraColorLayer.opacity = self.extraColorLayerOpacity;
[self.layer addSublayer:self.extraColorLayer];
}
self.extraColorLayer.backgroundColor = barTintColor.CGColor;

layoutSubviews

  [super layoutSubviews];
if (self.extraColorLayer != nil) {
[self.extraColorLayer removeFromSuperlayer];
self.extraColorLayer.opacity = self.extraColorLayerOpacity;
[self.layer insertSublayer:self.extraColorLayer atIndex:1];
CGFloat spaceAboveBar = self.frame.origin.y;
self.extraColorLayer.frame = CGRectMake(0, 0 - spaceAboveBar, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) + spaceAboveBar);
}

Matching the color of the 'transparent' navbar

I'm looking at this same problem now and am using the layer inspector to look at the navigation bar.

As it turns out, the UINavigationBar actually has two colored layers in it. One of them is based on your color, and one is a semitransparent near-white layer.

Have a look at this screenshot:

Sample Image

There's two layers.

The top layer (marked as A) has my color, in this case, a blue defined by UIColor(red: 0.13, green: 0.20, blue: 0.62, alpha: 1.00). This layer is set to an opacity of .85.

Beneath that, there's a second layer, marked as B, with a near-white with a background color defined as UIColor(white: 0.97, alpha: 0.5).

I haven't worked out how to imitate this in my library that I'm developing, but this screenshot has given me something to work with. I also am not sure how to mimic the borders, but this should be a great starting point.

Edit: As I said, here's some more info.

The Bottom Border:

I spent some more time on this yesterday, and I found a few interesting things: The bottom border of a UINavigationBar is actually a half-pixel tall UIImageView. It has no image, but it has a background color that's defined as UIColor(red: 0.0, green: 0.0, blue:0.0, alpha: 0.3).

I wasn't sure why they used an empty image view instead of a UIView but I have a theory now. When I set the border on my FilterBar view, I noticed that my color, defined exactly the same way, was not the same shade as the one in the UINavigationBar.

First, I realized that I was setting the border and the near-white color on the FilterBar itself, and the barTintColor was in a second layer. The near-white was bleeding into the border, so I added another layer for the white.

My border was still the wrong color. So, I dug deeper. I started printing the view objects into the debugger and I noticed that my border color was defined in the CGColorSpaceGray color space, and the UIImageView was defined in CGColorSpaceRGB.

Sample Image

To solve this problem, I manually created the color space I needed and then the transparent black color. On the device, it works perfectly, but on the simulator it's still a little off.

Here's what I used to make that color, in Swift:

    let space : CGColorSpace = CGColorSpaceCreateDeviceRGB()
let color : CGColor = CGColorCreate(space, [0.0, 0.0, 0.0, 0.3])

Setting the layer's borderColor to color and borderWidth to 0.5, I see a near identical view to the native navigation bar. I think this is why the UINavigationBar uses a UIImageView for the border. Although I haven't tested it yet, I suspect that it's there for the color space.

Sample Image

Layers:

I noted earlier that UINavigationBar is comprised of three layers. Well, actually, it's the private class _UINavigationBarBackground that's comprised of several layers, and that backdrop is only present if the navigation bar has translucency enabled.

If translucency is disabled, the UINavigationBar will apply the color to itself and hide the backdrop hierarchy. I've mimicked that behavior in FilterBar.

Sample Image

Summary:

So to summarize, the steps you need to take to mimic the UINavigationBar:

  1. For color, you want to add two layers to your view. The topmost layer takes your barTintColor, and has an 85% opacity. For an opaque version, remove the extra layers and set the color directly.

  2. To match the border, use a black color with 30% opacity, make sure you have the correct color space and make sure that your backdrop layers don't overlap, or they'll mess with your border.

  3. If you really want to match the behavior of the UINavigationBar, make your backdrop view stretch upward, beneath the status bar.

You can see my implementation in FilterBar on GitHub.

Transparent iOS navigation bar

You can apply Navigation Bar Image like below for Translucent.

Objective-C:

[self.navigationController.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault]; //UIImageNamed:@"transparent.png"
self.navigationController.navigationBar.shadowImage = [UIImage new];////UIImageNamed:@"transparent.png"
self.navigationController.navigationBar.translucent = YES;
self.navigationController.view.backgroundColor = [UIColor clearColor];

Swift 3:

self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default) //UIImage.init(named: "transparent.png")
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.view.backgroundColor = .clear

How do you make a navigation bar colored and translucent (iOS)?

The colour changing part comes from here. I just added the blur part from here. I do not know if it is the best solution for blur, but it is working. You will need to subclass your navigation bar, but nothing painful. Found it better if blur view had slightly dropped alpha, you will have to play with this a little.

extension UIColor {
func toImage() -> UIImage? {
return toImageWithSize(size: CGSize(width: 1, height: 1))
}
func toImageWithSize(size: CGSize) -> UIImage? {
UIGraphicsBeginImageContext(size)

if let ctx = UIGraphicsGetCurrentContext() {
let rectangle = CGRect(x: 0, y: 0, width: size.width, height: size.height)
ctx.setFillColor(self.cgColor)
ctx.addRect(rectangle)
ctx.drawPath(using: .fill)
let colorImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return colorImage
} else {
return nil
}
}
}

extension UIImage {
func imageWithAlpha(alpha: CGFloat) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(size, false, scale)
draw(at: CGPoint.zero, blendMode: .normal, alpha: alpha)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
}

class CustomNavBar: UINavigationBar {

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)

setBackgroundImage(UIColor.blue.toImage()?.imageWithAlpha(alpha: 0.5), for: .default)
addBlurEffect()
}

func addBlurEffect() {
let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
var frame = bounds
frame.origin.y -= 20
frame.size.height += 20
visualEffectView.frame = frame
visualEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
visualEffectView.alpha = 0.9
insertSubview(visualEffectView, at: 0)
sendSubview(toBack: visualEffectView)
}
}

How to set transparency of translucent navigation bar in iOS7?

It's late, but if anyone need the answer here it is.


If you want transparency in all navigationBar than write following lines in your AppDelegate's
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
method:

## Objective C ##

[[UINavigationBar appearance] setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[UIImage new]];
[[UINavigationBar appearance] setTranslucent:YES];

## Swift ##

UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().translucent = true

For transparency in particular view, check this answer.



Related Topics



Leave a reply



Submit