iOS 7.1 UPDATE:似乎在此更新中忽略了在UINavigationBar中修改Alpha通道的解决方法.现在,最好的解决方案似乎就是"处理它",希望你 Select 的任何 colored颜色 都能呈现出半透明的效果.我仍在寻找绕过这一问题的方法.
iOS 7.0.3 UPDATE:GitHub library we created已更新,在使用iOS 7.0.3时稍微解决了此问题.不幸的是,没有神奇的公式可以同时支持在iOS 7.0.2及更早版本中创建的 colored颜色 和iOS 7.0.3中创建的 colored颜色 .苹果似乎改善了饱和度,但代价是不透明度(因为模糊的半透明取决于不透明度级别).我和其他几个人一起,正在努力为这个问题创建一个更好的修复方案.
我相信很多人已经遇到过iOS7倾向于降低半透明UINavigationBar colored颜色 的问题.
我的目标是实现具有此色调但半透明的UINavigationBar:
然而,在半透明的情况下,我得到了这个.背景视图是白色的,我理解这会使此视图稍微亮一点:
有没有办法既保持原色又保持半透明呢?我注意到Facebook已经能够把他们的条形图变成丰富的蓝色,如下所示:
..所以我知道一定有什么办法.背景视图在这里显然有不同之处,但它们的大部分内容也是灰色/白色的.似乎无论你加入什么色调,你都无法在半透明状态下获得鲜艳的色彩.
Updated with solution.
这是我最后想出的解决方案.我采用了aprato的解决方案,然后将自定义UINavigationBar
包含在UINavigationController
子类中.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