gpt4 book ai didi

iphone - NavigationBar 图层渐变效果不佳

转载 作者:行者123 更新时间:2023-11-28 20:40:04 28 4
gpt4 key购买 nike

我正在向导航栏层添加渐变,并且效果很好。当我推送另一个 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com