【问题标题】:Adding CAGradient layer but unable to get Gradient effect添加CAGradient图层但无法获得渐变效果
【发布时间】:2016-11-04 09:43:03
【问题描述】:

这里我正在使用此代码来提供渐变效果,但根本没有得到效果。

 CAGradientLayer *layer2 = [CAGradientLayer layer];

 NSArray *gradientColors = [NSArray arrayWithObjects:(id)[self colorWithHexString:@"3eb79d"].CGColor,(id)[self colorWithHexString:@"3fb65c"].CGColor,(id)[self colorWithHexString:@"3cb7da"].CGColor, nil];

  [layer2 setColors:gradientColors];
  [layer2 setFrame:cell.userBackgroundView.layer.frame];
  [cell.userBackgroundView.layer insertSublayer:layer2 atIndex:0];
  cell.userBackgroundView.clipsToBounds = YES;

【问题讨论】:

    标签: ios objective-c gradient


    【解决方案1】:

    试试这个

    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame =cell.userBackgroundView.frame;
    gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor clearColor] CGColor], (id)[[UIColor blackColor] CGColor], nil];
    [cell.userBackgroundView.layer insertSublayer:gradient atIndex:0];
    

    【讨论】:

      【解决方案2】:

      尝试将子层的索引从0更改为-1

      [cell.userBackgroundView.layer insertSublayer:layer2 atIndex:-1];
      

      确保您的 userBackgroundView 未被其他视图覆盖。

      【讨论】:

        【解决方案3】:

        使用

        [layer2 setFrame:cell.userBackgroundView.bounds];
        

        而不是

        [layer2 setFrame:cell.userBackgroundView.layer.frame];
        

        如果您使用自动布局,请确保将框架设置为viewDidAppearviewDidLayOutSubviews

        【讨论】:

          猜你喜欢
          • 2017-06-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-28
          • 2013-11-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多