ios 在 iOS7 半透明导航栏中获取正确的颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18895252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the right color in iOS7 translucent navigation bar
提问by stk
How can I get the right coloring for my translucent navigation bars in iOS 7? The navigation bar just adjusts the given color to a much brighter one. Changing brightness or saturation of the color also doesn′t deliver the right result.
如何在 iOS 7 中为我的半透明导航栏获得正确的颜色?导航栏只是将给定的颜色调整为更亮的颜色。改变颜色的亮度或饱和度也不能提供正确的结果。
Anyone having the same trouble? It seems to work somehow, looking at Facebook: they′re having their colors and translucent navigation bars.
有人有同样的烦恼吗?看看 Facebook,它似乎以某种方式起作用:它们有自己的颜色和半透明的导航栏。
Edit: Just to make it clear: I need the Bar to be translucent, not transparent (with some alpha), not solid! http://en.wikipedia.org/wiki/Transparency_and_translucency
编辑:只是为了清楚起见:我需要 Bar 是半透明的,不透明的(带有一些 alpha),而不是实心的!http://en.wikipedia.org/wiki/Transparency_and_translucency
Edit: Now posted to Apple BugReporter
编辑:现在发布到 Apple BugReporter
回答by SomeGuy
The bar will adjust your color values.
该条将调整您的颜色值。
Preferred method, for RGB >= 40 only, will give the most blurring
首选方法,仅对于 RGB >= 40,将产生最大的模糊
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
您可以使用此计算器并在屏幕上渲染时输入您想要的颜色,它会告诉您设置 barTintColor 的颜色,因此当 Apple 调整它时,它会按预期显示
https://www.transpire.com/insights/blog/bar-color-calculator/
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)
编辑:请注意,这些计算适用于白色背景和较浅的颜色(rgb 超过 40,如果您需要更深的颜色,则需要像其他人提到的那样添加背景层 - 尽管这会减少条形的模糊)
In depth guide: https://www.transpire.com/insights/blog/custom-ui-navigationbar-colors-ios7/
深度指南: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]];
回答by malex
You simply need to change translucent
property
您只需要更改translucent
属性
navigationBar.translucent = NO;
It is effectively the same as removing/making transparent subviews/sublayers of the navigation bar.
它实际上与删除/制作导航栏的透明子视图/子层相同。
回答by Allen Hsu
I've improved code from Achieving bright, vivid colors for an iOS 7 translucent UINavigationBarin my fork: https://github.com/allenhsu/CRNavigationController
我已经改进了在我的 fork 中为 iOS 7 半透明 UINavigationBar 实现明亮、鲜艳的颜色的代码:https: //github.com/allenhsu/CRNavigationController
With my modification, the result color on screen (picked on white background) will be exactly the same value passed into setBarTintColor. I think it's an amazing solution.
通过我的修改,屏幕上的结果颜色(在白色背景上选择)将与传递给 setBarTintColor 的值完全相同。我认为这是一个了不起的解决方案。
回答by Elethier
I know this answer is a little late, but if you're using Interface Builder, you might be getting the wrong color when using a hex value because Interface Builder is set to use the wrong color space. In Xcode 6.4, you can press the little gear in the top right of the color picker dialog to pick which color space you're using:
我知道这个答案有点晚了,但是如果您使用的是 Interface Builder,则在使用十六进制值时可能会得到错误的颜色,因为 Interface Builder 设置为使用错误的颜色空间。在 Xcode 6.4 中,您可以按颜色选择器对话框右上角的小齿轮来选择您正在使用的颜色空间:
Mine was set to sRGB IEC6196-2.1, when I actually should have been using Generic RGB.
我的设置为 sRGB IEC6196-2.1,而我实际上应该使用 Generic RGB。
回答by Matthew Crenshaw
If your color isn't exceptionally vivid, you can calculate the equivalent color w/ alpha. This works well in iOS 7.0.3+; prior to 7.0.3 it automatically applied a 0.5 alpha.
如果您的颜色不是特别鲜艳,您可以计算带 alpha 的等效颜色。这在 iOS 7.0.3+ 中运行良好;在 7.0.3 之前,它会自动应用 0.5 alpha。
This code assumes that your input color is RGB and is opaque, and that your background color is white:
此代码假定您的输入颜色是 RGB 并且是不透明的,并且您的背景颜色是白色:
- (UIColor *) colorByInterpolatingForBarTintWithMinimumAlpha: (CGFloat) alpha
{
NSAssert(self.canProvideRGBComponents, @"Self must be a RGB color to use arithmatic operations");
NSAssert(self.alpha == 1, @"Self must be an opaque RGB color");
CGFloat r, g, b, a;
if (![self getRed:&r green:&g blue:&b alpha:&a]) return nil;
CGFloat r2,g2,b2,a2;
r2 = g2 = b2 = a2 = 1;
CGFloat red,green,blue;
alpha -= 0.01;
do {
alpha += 0.01;
red = (r - r2 + r2 * alpha) / alpha;
green = (g - g2 + g2 * alpha) / alpha;
blue = (b - b2 + b2 * alpha) / alpha;
} while (alpha < 1 && (red < 0 || green < 0 || blue < 0 || red > 1 || green > 1 || blue > 1));
UIColor *new = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
return new;
}
If anyone has a more elegant way of calculating the alpha (I'm cringing at that do-while loop) I'd love to see it: https://gist.github.com/sgtsquiggs/7206385
如果有人有更优雅的计算 alpha 的方法(我对那个 do-while 循环感到畏缩),我很乐意看到它:https: //gist.github.com/sgtsquiggs/7206385
回答by Damien Romito
Just use this simple barTintColor Calculator:
只需使用这个简单的 barTintColor Calculator:
回答by Markymark
Simple/fast solution that worked for me. Just set the bar tint in the storyboard rather than the background.
对我有用的简单/快速解决方案。只需在故事板而不是背景中设置条形色调。
First select your Navigation Bar in your Navigation Controller
And then click the attributes inspector on the right and then set the Bar Tint
You can select a pre-defined color or click on the color to set it to something else.
回答by IvanRublev
Here is another way to get right color of translucent navigation bar in iOS 7.x and later. For some colors it's possible to find the optimal bar tint color that makes translucent bar to appear with color that matches the desired one.
这是在 iOS 7.x 及更高版本中获得半透明导航栏正确颜色的另一种方法。对于某些颜色,可以找到最佳条着色颜色,使半透明条显示为与所需颜色相匹配的颜色。
For example, for Facebook color that is rgb: 65,96,156
or #41609c
the optimal color is #21458c
. The following code set all navigation bars in the app to be of Facebook color with native cocoa-touch API only:
例如,对于 Facebook 颜色是rgb: 65,96,156
或#41609c
最佳颜色是#21458c
。以下代码仅使用原生 cocoa-touch API 将应用程序中的所有导航栏设置为 Facebook 颜色:
UIColor* barColor = [UIColor colorWithRed:0.129995 green:0.273324 blue:0.549711 alpha:1.0]; // #21458c to make bars actual color match the #41609c color.
[[UINavigationBar appearance] setBarTintColor:barColor];
The only limitation of the method is that optimized color can't be found for every possible color. Usually this is not possible for dark colors.
该方法的唯一限制是无法为每种可能的颜色找到优化的颜色。通常这对于深色是不可能的。
I made an BarTintColorOptimizerutility that should be run on the device to search for optimized bar color for any color you enter.
我制作了一个BarTintColorOptimizer实用程序,它应该在设备上运行,以便为您输入的任何颜色搜索优化的条形颜色。
回答by gleb.kudr
I suppose you have read all the comments above. If you want to get the custom background & translucency you should override the navigationbar class and implement your own layoutsubviews method. Simple add additional subview here. IMPORTANT: you should add it just above the background subview of the NavigationBar. It will hide your header or buttons if you just put it above the all subviews.
我想你已经阅读了上面的所有评论。如果你想获得自定义背景和半透明,你应该覆盖导航栏类并实现你自己的 layoutsubviews 方法。简单地在此处添加额外的子视图。重要提示:您应该将它添加到 NavigationBar 的背景子视图上方。如果您将其放在所有子视图上方,它将隐藏您的标题或按钮。
Also, check out this question
另外,看看这个问题
回答by icekomo
If you're using swift 2.0 you can use this, this will remove the blur and have the color show properly.
如果您使用的是 swift 2.0,您可以使用它,这将消除模糊并正确显示颜色。
UINavigationBar.appearance().translucent = false