Achieving bright, vivid colors for an iOS 7 translucent UINavigationBar

问题:


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,但半透明:

UINavigationBar,Opaque

However, with translucency, I'm getting this. 然而,有了半透明,我得到了这个。 The background view is white, which I understand will make this view a bit lighter: 背景视图是白色的,据我所知会使这个视图更轻一些:

UINavigationBar,半透明

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已经能够让他们的酒吧变成他们丰富的蓝色,如下所示:

Facebook UINavigationBar,半透明

..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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章