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:

UINavigationBar,不透明

然而,在半透明的情况下,我得到了这个.背景视图是白色的,我理解这会使此视图稍微亮一点:

有没有办法既保持原色又保持半透明呢?我注意到Facebook已经能够把他们的条形图变成丰富的蓝色,如下所示:

Facebook UINavigationBar,半透明

..所以我知道一定有什么办法.背景视图在这里显然有不同之处,但它们的大部分内容也是灰色/白色的.似乎无论你加入什么色调,你都无法在半透明状态下获得鲜艳的色彩.

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

推荐答案

正如您在7.0.3以上看到的,情况发生了变化.我已经更新了我的要点.希望这种情况会随着人们的升级而消失.

Original Answer:个 我最终得到了一个将其他两个答案结合在一起的黑客.我子类化了UINavigationBar,并在后面添加了一个层,如果有任何不同的高度状态栏是向上的,就会有一些额外的空间来覆盖.无论何时设置barTintColor,图层都会在布局子视图中进行调整,并且 colored颜色 会更改.

要点:https://gist.github.com/aprato/6631390

colored颜色

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

布局子视图

  [super 布局子视图];
  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);
  }

Ios相关问答推荐

如何防止UITest套件与Fastlane一起执行?

如何在HStack中将sfsymbol与Text视图对齐?

如何在 Flutter 应用程序中测试来自 App/Play Store 的移动深层链接?

如何结合`@dynamicMemberLookup`和`ExpressibleByStringInterpolation`来实现方法链?

如何从单独的视图更改修饰符的值

Flutter firebase_auth 中的无效应用程序凭据/令牌不匹配

将角半径仅应用于 Swiftui 中按钮的两个角

如何在保持纵横比的同时zoom UIView 的内容以适应目标矩形?

缺少推送通知权利

如何在通用应用程序中同时支持 iPad 和 iPhone 视网膜图形

如何在 UIActivityViewController 中设置邮件主题?

如何从一个特定的视图控制器中隐藏导航栏

如何比较两个 UIImage 对象

UIView 和 initWithFrame 以及一个 NIB 文件.如何加载 NIB 文件?

所有小部件的Flutter 填充?

Xcode / iOS模拟器:手动触发重大位置更改

Swift - 获取设备的 WIFI IP 地址

如何在 iOS 中获取正在运行的应用程序的名称

将视图控制器从一个情节提要移动或复制到另一个情节提要

如何立即移动 CALayer(无动画)