旋转背景渐变xcode

时间:2014-12-05 12:34:44

标签: ios objective-c cagradientlayer

我的应用上的背景渐变有问题。我在viewDidLoad中设置MainViewController

CAGradientLayer *bgLayer = [BackgroundGradient blackGradient];
bgLayer.frame = self.view.bounds;
[self.view.layer insertSublayer:bgLayer atIndex:0];

每个UIViewController都来自MainViewController。它工作正常,但旋转设备后,它只显示我的一半背景。其他是白色的。你能帮我吗?问题出在哪里,并在旋转后只显示一半?example

在BackgroundGradient.m

@implementation BackgroundGradient
+ (CAGradientLayer*) blackGradient {

    UIColor *colorOne = [UIColor colorWithRed:0.12 green:0.13 blue:0.13 alpha:1.0];
    UIColor *colorTwo = [UIColor colorWithRed:0.11 green:0.12 blue:0.13 alpha:1.0];

    NSArray *colors = [NSArray arrayWithObjects:(id)colorOne.CGColor, colorTwo.CGColor, nil];
    NSNumber *stopOne = [NSNumber numberWithFloat:0.0];
    NSNumber *stopTwo = [NSNumber numberWithFloat:1.0];

    NSArray *locations = [NSArray arrayWithObjects:stopOne, stopTwo, nil];

    CAGradientLayer *headerLayer = [CAGradientLayer layer];
    headerLayer.colors = colors;
    headerLayer.locations = locations;

    return headerLayer;
}

1 个答案:

答案 0 :(得分:1)

将图层保存到控制器属性中,而不是覆盖layoutSubviews方法并更新图层框架。

- (void)layoutSubviews {
    [super layoutSubviews];
    self.backgroundLayer.frame = self.view.bounds;
}