CALayer不会针对不同方向使用渐变颜色进行自动调整

时间:2015-10-27 05:00:37

标签: ios objective-c autolayout orientation cagradientlayer

我使用自动布局约束以编程方式创建一些自定义视图,并希望将渐变颜色设置为自定义视图的背景颜色。看来如果我没有为自定义视图设置框架,则自定义视图不会显示背景颜色。如果我设置了一个帧,则背景渐变颜色仅出现在帧大小中,但不会调整到视图边界以及不同的方向。我应该怎么做才能将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

1 个答案:

答案 0 :(得分:0)

您需要使用视图来管理CAGradientLayer。从this answer复制GradientView课程,并按照以下方式使用:

@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];
}