0

我的应用程序的背景渐变有问题。我进去viewDidLoadMainViewController

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

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

在 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;
}
4

1 回答 1

1

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

- (void)layoutSubviews {
    [super layoutSubviews];
    self.backgroundLayer.frame = self.view.bounds;
}
于 2014-12-05T13:47:17.093 回答