問題:
iOS 7.1 UPDATE : Looks like the workaround for modifying the alpha channel in the UINavigationBar has been ignored in this update. iOS 7.1更新 :在此更新中,似乎忽略了修改UINavigationBar中Alpha通道的變通方法。 Right now, the best solution seems to be to just 'deal with it' and hope that whatever color you choose can render a translucent effect. 現在,最好的解決方案似乎只是“處理它”,並希望你選擇的任何顏色都可以呈現半透明效果。 I am still looking into ways of getting around this. 我仍然在尋找解決這個問題的方法。
iOS 7.0.3 UPDATE : The GitHub library we created has been updated to slightly work around this issue when using iOS 7.0.3. iOS 7.0.3更新 :使用iOS 7.0.3時, 我們創建的GitHub庫已經更新,可以解決此問題。 Unfortunately, there is no magic formula to support both colors created in iOS 7.0.2 and earlier and iOS 7.0.3. 不幸的是,沒有神奇的公式可以支持iOS 7.0.2及更早版本和iOS 7.0.3中創建的顏色。 Seems like Apple improved the saturation, but at the cost of opacity (since the blurred translucency is dependant on the opacity level). 似乎Apple改善了飽和度,但是以不透明度爲代價(因爲模糊的半透明度取決於不透明度水平)。 I, along with a few others, are working on creating a much better fix for this. 我和其他一些人正在努力爲此創建更好的解決方案。
I'm sure many people have already come across the problem where iOS 7 tends to desaturate the color of a UINavigationBar that is translucent. 我相信很多人已經遇到過這樣的問題:iOS 7傾向於使半透明的UINavigationBar的顏色飽和。
My goal is to achieve a UINavigationBar with this tint color, but translucent: 我的目標是使用這種色調實現UINavigationBar,但半透明:
However, with translucency, I'm getting this. 然而,有了半透明,我得到了這個。 The background view is white, which I understand will make this view a bit lighter: 背景視圖是白色的,據我所知會使這個視圖更輕一些:
Is there any way to achieve the original color while still having translucency? 有沒有辦法在保持半透明的同時達到原色? I've noticed Facebook has been able to get their bar to be their rich, blue color, as displayed here: 我注意到Facebook已經能夠讓他們的酒吧變成他們豐富的藍色,如下所示:
..so I know there has to be some way. ..所以我知道必須有某種方式。 Background views obviously make a difference here, but most of their content is also gray/white. 背景視圖在這裏顯然有所不同,但其大部分內容也是灰色/白色。 It seems that regardless of whatever bar tint color you put in, you are unable to get vivid colors under translucency. 似乎無論你放入什麼樣的色調,你都無法在半透明的情況下獲得鮮豔的色彩。
Updated with solution. 更新瞭解決方案。
Here's the solution that I ended up coming up with. 這是我最終提出的解決方案。 I took aprato 's solution and then encompassed the custom UINavigationBar
within a UINavigationController
subclass. 我採用了aprato的解決方案,然後在UINavigationController
子類中包含了自定義UINavigationBar
。 I have created a repository that has this implementation listed below, along with an example app . 我創建了一個具有下面列出的實現的存儲庫,以及一個示例應用程序 。
////////////////////////////
// CRNavigationBar.m
////////////////////////////
#import "CRNavigationBar.h"
@interface CRNavigationBar ()
@property (nonatomic, strong) CALayer *colorLayer;
@end
@implementation CRNavigationBar
static CGFloat const kDefaultColorLayerOpacity = 0.5f;
static CGFloat const kSpaceToCoverStatusBars = 20.0f;
- (void)setBarTintColor:(UIColor *)barTintColor {
[super setBarTintColor:barTintColor];
if (self.colorLayer == nil) {
self.colorLayer = [CALayer layer];
self.colorLayer.opacity = kDefaultColorLayerOpacity;
[self.layer addSublayer:self.colorLayer];
}
self.colorLayer.backgroundColor = barTintColor.CGColor;
}
- (void)layoutSubviews {
[super layoutSubviews];
if (self.colorLayer != nil) {
self.colorLayer.frame = CGRectMake(0, 0 - kSpaceToCoverStatusBars, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) + kSpaceToCoverStatusBars);
[self.layer insertSublayer:self.colorLayer atIndex:1];
}
}
@end
////////////////////////////
// CRNavigationController.m
////////////////////////////
#import "CRNavigationController.h"
#import "CRNavigationBar.h"
@interface CRNavigationController ()
@end
@implementation CRNavigationController
- (id)init {
self = [super initWithNavigationBarClass:[CRNavigationBar class] toolbarClass:nil];
if(self) {
// Custom initialization here, if needed.
}
return self;
}
- (id)initWithRootViewController:(UIViewController *)rootViewController {
self = [super initWithNavigationBarClass:[CRNavigationBar class] toolbarClass:nil];
if(self) {
self.viewControllers = @[rootViewController];
}
return self;
}
@end
解決方案:
參考一: https://en.stackoom.com/question/1HI69參考二: https://stackoom.com/question/1HI69