设置导航栏渐变色

以下方法可用来设置导航栏上的渐变色。


- (void)setNavigationBarBackgroundColor{

    //  创建 UIView用来承载渐变色放置在导航栏上时需要上移20否则状态栏会露出

    UIView *myTopView = [[UIView alloc]initWithFrame:CGRectMake(0, -20,WIDTH ,64)];



//1、window 没有设置导航控制器为根视图的情况下

#if 0 

[self.view addSubview:myTopView];

//2、设置了导航控制器为根视图。在AppDelegate中设置了

    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

    self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[ViewController new]];

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

#else  //则视图添加在navigationBar上 或者其他方法设置在navigationBar处

    [self.navigationController.navigationBar addSubview:myTopView];

#endif



    // 创建渐变色图层

    CAGradientLayer *gradientLayer= [CAGradientLayer layer];

    gradientLayer.frame       =CGRectMake(0,0,WIDTH,64);

  

    gradientLayer.colors =@[

                             (id)[UIColor yellowColor].CGColor,

                             (id)[UIColor cyanColor].CGColor

                             ];

    /* The start and end points of the gradient when drawn into the layer's

     * coordinate space. The start point corresponds to the first gradient

     * stop, the end point to the last gradient stop. Both points are

     * defined in a unit coordinate space that is then mapped to the

     * layer's bounds rectangle when drawn. (I.e. [0,0] is the bottom-left

     * corner of the layer, [1,1] is the top-right corner.) The default values

     * are [.5,0] and [.5,1] respectively. Both are animatable.

     */

    // 设置渐变方向(0~1)默认位置为 (0.5,0) 和 (0.5,1)

    gradientLayer.startPoint =CGPointMake(0,0);

    gradientLayer.endPoint =CGPointMake(0,1);

    

    // 设置渐变色的起始位置和终止位置(颜色的分割点)

    /* An optional array of NSNumber objects defining the location of each

     * gradient stop as a value in the range [0,1]. The values must be

     * monotonically increasing. If a nil array is given, the stops are

     * assumed to spread uniformly across the [0,1] range. When rendered,

     * the colors are mapped to the output colorspace before being

     * interpolated. Defaults to nil. Animatable. */

    gradientLayer.locations =@[@(0.15f),@(0.98f)];

    //渐变色图层的线宽

    gradientLayer.borderWidth  =0.0;

    

    // 添加图层

    [myTopView.layer addSublayer:gradientLayer];

}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值