CALayer 不会针对不同的方向使用渐变颜色自动调整大小

CALayer does not autoresizing with gradient color for different orientation

我正在使用自动布局约束以编程方式创建一些自定义视图,并希望将渐变色设置为自定义视图的背景色。似乎如果我没有为自定义视图设置框架,那么自定义视图就不会出现背景颜色。如果我设置一个框架,那么背景渐变颜色仅针对框架大小出现,但不会调整到视图边界以及不同方向。我应该怎么做才能将 CALayer 的大小调整到具有不同方向支持的视图边界?我试过如下 -

- (void) addGradientBackgroundColorToCustomView:(UIView *)view {
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = view.bounds;

    gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor redColor] CGColor], (id)[[UIColor greenColor] CGColor], nil];

    [view.layer insertSublayer:gradient atIndex:0];
    //[view.layer addSublayer:gradient atIndex:0];
}

- (void)addCustomViewAsRow {

    UIView *lastView;
    for (int i = 0; i<5; i++) {

        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.layer.bounds.size.width - 20, 50)];

        //If i uncomment the following line and comment out the previous line then nothing appears as background color

        //UIView *view = [[UIView alloc] init];

        //Adding gradient color to the custom view
        [self addGradientBackgroundColorToCustomView:view];

        view.translatesAutoresizingMaskIntoConstraints = NO;
        [self.view addSubview:view];

        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[view]-10-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)]];

        [self.view addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1 constant:-20.0]];

        if (i == 0) {
            [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view(50)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)]];
        } else {
            [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[lastView(50)]-10-[view(50)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(lastView, view)]];
        }

        lastView = view;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self addCustomViewAsRow];
}

已编辑:

我也试过像下面的代码片段但对我不起作用 -

在我的TestViewController.m-

#import "TestViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "GradientView.h"

#define DEFAULT_ROW_HEIGHT 50

@interface TestViewController ()

@property (nonatomic, strong) CAGradientLayer *gradientView;

@end

@implementation ViewController

- (void) addGradientBackgroundColorToCustomView:(UIView *)view {

    self.gradientView = [[GradientView layerClass] layer];
    self.gradientView.colors = [NSArray arrayWithObjects:(id)[[UIColor redColor] CGColor], (id)[[UIColor greenColor] CGColor], nil];

    //[view.layer insertSublayer:self.gradientView atIndex:0];

    [view.layer addSublayer:self.gradientView];

}

- (void) addCustomViewAsRow {

    UIView *lastView;

    for (int i = 0; i<5; i++) {

        //UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.layer.bounds.size.width -20, 50)];

        UIView *view = [[UIView alloc] init];

        //Adding gradient color to the custom view
        [self addGradientBackgroundColorToCustomView:view];

        view.translatesAutoresizingMaskIntoConstraints = NO;
        [self.view addSubview:view];

        //Adding horizontal layout for the custom view
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[view]-10-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)]];

        [self.view addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1 constant:-20.0]];

        //Adding vertical layout for the custom view
        if (i == 0) {
            [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view(50)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)]];
        } else {
            [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[lastView(50)]-10-[view(50)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(lastView, view)]];
        }

        lastView = view;

    }
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    //self.gradientView.frame = self.view.bounds;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self addCustomViewAsRow];
}

@end

在GradientView.h-

#import <UIKit/UIKit.h>

@interface GradientView : UIView

+(Class) layerClass;

@end

在GradientView.m-

#import "GradientView.h"
#import <QuartzCore/QuartzCore.h>

@implementation GradientView

+(Class) layerClass {

    return [CAGradientLayer class];
}

@end

您需要使用视图来管理您的 CAGradientLayer。从 this answer 复制 GradientView class,然后像这样使用它:

@implementation TestViewController

- (void) addGradientBackgroundColorToCustomView:(UIView *)view {
    GradientView *gradientView = [[GradientView alloc] initWithFrame:view.bounds];
    gradientView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

    CAGradientLayer *layer = (CAGradientLayer *)gradientView.layer;
    layer.colors = [NSArray arrayWithObjects:(id)[[UIColor redColor] CGColor], (id)[[UIColor greenColor] CGColor], nil];

    [view addSubview:gradientView];
}