-1

我需要知道我做錯了什麼。我字面上複製並粘貼到我的代碼和梯度不顯示。我的CAGradientLayer未顯示?

#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

你可能打算踢自己:)

梯度層需要CGColorRef,不是的UIColor:

CGColorRef topRef = [UIColor redColor].CGColor; 
CGColorRef botRef = [UIColor blueColor].CGColor; 

[gradient_layer setColors:@[(__bridge id)topRef, (__bridge id)botRef]]; 
+0

Im相當肯定我這樣做了,不過生病嘗試再次UT斯達康出來 – Esko918