ios背景颜色仅在横向模式下显示在屏幕的一半上

时间:2015-04-17 07:44:54

标签: ios background landscape-portrait

我目前正在使用以下方法设置视图的背景颜色

 //Set the background color
    CAGradientLayer *bgLayer = [BackgroundLayer blueGradient]; <---Calls method below
    bgLayer.frame = self.view.bounds;
    //self.view.layer.backgroundColor = [UIColor redColor].CGColor;
    [self.view.layer insertSublayer:bgLayer atIndex:0];

这是获取渐变颜色的方法 - 这是上面调用的方法

@implementation BackgroundLayer
//Blue gradient background
+ (CAGradientLayer*) blueGradient {

    UIColor *colorOne = [UIColor colorWithRed:(120/255.0) green:(135/255.0) blue:(150/255.0) alpha:1.0];
    UIColor *colorTwo = [UIColor colorWithRed:(57/255.0)  green:(79/255.0)  blue:(96/255.0)  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;
}

@end

现在处于纵向模式,背景显示正常。但是在横向模式下,它只覆盖屏幕的一半而另一半是白色。这是为什么 ?我该如何解决呢?我尝试了以下更改颜色

self.view.layer.backgroundColor = [UIColor redColor].CGColor;

以上工作在横向和纵向模式下均可正常工作,但不是渐变。关于如何使梯度工作的任何想法?

4 个答案:

答案 0 :(得分:2)

简单方法:

只需将其添加到您的viewcontroller

即可
-(void)viewDidLayoutSubviews{
    self.bgLayer.frame = self.view.bounds;
}

更好的方式:

作为here描述,你可以这样做。我用My Xcode测试,它运行正常。

  1. 定义背景视图以管理尺寸
  2. .h文件

        #import <UIKit/UIKit.h>
    
        @interface BackgrundView : UIView
    
        @end
    

    .m文件

        #import "BackgrundView.h"      
        @interface BackgrundView()
        @end
        @implementation BackgrundView
        +(Class)layerClass{
            return [CAGradientLayer class];
        }
        @end
    

    2设置你想要的东西

    #import "ViewController.h"
    #import "BackgrundView.h"
    @interface ViewController ()
    @property BackgrundView * backgroundview;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.backgroundview = [[BackgrundView alloc] initWithFrame:self.view.frame];
        self.backgroundview.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        [self.view addSubview:self.backgroundview];
        UIColor *colorOne = [UIColor colorWithRed:(120/255.0) green:(135/255.0) blue:(150/255.0) alpha:1.0];
        UIColor *colorTwo = [UIColor colorWithRed:(57/255.0)  green:(79/255.0)  blue:(96/255.0)  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 * graintLayer = (CAGradientLayer *)self.backgroundview.layer;
        graintLayer.colors = colors;
        graintLayer.locations = locations;
    }
    

答案 1 :(得分:0)

正确的代码行是:self.view.backgroundColor = [UIColor redColor];

答案 2 :(得分:0)

当设备旋转到横向时,您是否更新背景图层的框架? 显然你的问题可以从这个问题here

的答案中解决

答案 3 :(得分:0)

您需要在设备旋转时添加新渐变。

- (void)viewDidLayoutSubviews // called when device rotates
{
    [self.bgLayer removeFromSuperlayer]; // you need to keep reference so you can remove the old layer

    self.bgLayer = [BackgroundLayer blueGradient]; <---Calls method below
    self.bgLayer.frame = self.view.bounds;
    [self.view.layer insertSublayer:self.bgLayer atIndex:0];
}
看看@WenchenHuang的答案,它比我的做法更好。