我需要知道我做错了什么。我在字面上将其复制并粘贴到我的代码中,并且不显示渐变。
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *gradient_view = [[UIView alloc]initWithFrame:self.view.bounds];
CAGradientLayer *gradient_layer = [CAGradientLayer layer];
[gradient_layer setFrame:gradient_view.bounds];
gradient_layer.startPoint = CGPointMake(0, 0);
gradient_layer.endPoint = CGPointMake(0, 1);
UIColor *top_color = [UIColor redColor];
UIColor *bottom_color = [UIColor blueColor];
[gradient_layer setColors:@[top_color,bottom_color]];
[gradient_view.layer addSublayer:gradient_layer];
[self.view addSubview:gradient_view];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
它只显示一个白色屏幕。任何人都可以帮我弄清问题是什么。因为它现在真的让我烦恼。
答案 0 :(得分:0)
你可能会踢自己:)
渐变层需要CGColorRef,而不是UIColor:
CGColorRef topRef = [UIColor redColor].CGColor;
CGColorRef botRef = [UIColor blueColor].CGColor;
[gradient_layer setColors:@[(__bridge id)topRef, (__bridge id)botRef]];