iphone - NavigationBar 图层渐变效果不佳

标签 iphone objective-c ios

我正在向导航栏层添加渐变,并且效果很好。 当我推送另一个 View Controller 和弹出 View Controller 时,问题就来了。父 View Controller 有右边的条形按钮项目,当我弹出 View Controller 时,它的颜色会变暗。 我的代码是

CGRect navFrame = self.navigationController.navigationBar.frame;
navFrame.origin.y = 0.0f;
[self.navigationController.navigationBar.layer insertSublayer:[AddGradient addGradientToNavigationBar:navFrame] atIndex:0];

addGradientToNavigationBar 的代码是

+ (CAGradientLayer*)addGradientToNavigationBar:(CGRect)navRect {

    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = navRect;
    gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0] CGColor],
                   (id)[[XAppDelegate getColor] CGColor], nil];

    return gradient;
}

最佳答案

我也遇到了同样的问题。 我试图使用 UIBarStyleBlackTranslucent 样式将渐变层添加到导航栏以获得玻璃效果。在网上搜索和大量实验后,我还没有找到解决该问题的正确方法。最后我想出了以下解决方法:

    CAGradientLayer *yourGradient = ...

//需要两个额外的层来模仿 UIBarStyleBlackTranslucent 风格

    CAGradientLayer *gradient = [CAGradientLayer layer];

    gradient.frame = CGRectMake(0, 0, 320, 44);


    NSMutableArray *cgColors = [[NSMutableArray alloc] init];

    [cgColors addObject:(id)[[UIColor colorWithRed:0.0
                                     green:0.0
                                      blue:0.0
                                     alpha:0.2] CGColor]];
    [cgColors addObject:(id)[[UIColor colorWithRed:0.0
                                     green:0.0
                                      blue:0.0
                                     alpha:0.6] CGColor]];
    [cgColors addObject:(id)[[UIColor colorWithRed:0.0
                                     green:0.0
                                      blue:0.0
                                     alpha:0.5] CGColor]];

    gradient.colors = cgColors;

    CALayer *layer = [CALayer layer];

    _layer.frame = CGRectMake(0, 22, 320, 22);

    _layer.backgroundColor =[[UIColor colorWithRed:0.0
                                             green:0.0
                                              blue:0.0
                                             alpha:0.2] CGColor];

//导航栏按钮需要这个

    [rootController.navigationBar setBarStyle:UIBarStyleBlackTranslucent];

//在导航栏层的第一个子层添加层

    [[[rootController.navigationBar.layer sublayers] objectAtIndex:0] insertSublayer:yourGradient atIndex:0];

//添加渐变层后,UIBarStyleBlackTranslucent 样式效果消失

//来自栏(但不是来自按钮)

//所以需要下面的来模仿一下

    [[[rootController.navigationBar.layer sublayers] objectAtIndex:0] insertSublayer:gradient atIndex:1];

    [[[rootController.navigationBar.layer sublayers] objectAtIndex:0] insertSublayer:layer atIndex:2];

您可能还想为图层添加一些边框,因为边框似乎消失了。

关于iphone - NavigationBar 图层渐变效果不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8988742/

相关文章:

iphone - iPhone 上的核心动画问题

iphone - 如何向 UITableView 添加页脚?

iphone - MKPolygon 触摸细节 View

javascript - 在 javascript 中调用原生的 swift 函数

java - 模拟器工具,适用于 Iphone、智能手机和任何手机初学者的编程环境

iphone - 使单元格 imageView 飞入动画

ios - 添加 addTarget : method to custom UIView

iphone - ASID下载缓存安全

ios - CoreBluetooth 状态保存 : correct way to restore CBCentralManager

ios - 检测用户何时拒绝访问相机