Unable to get background gradient to fill the whole screen when rotating

I have a background gradient that draws beautifully when the screen is initially displayed in portrait mode. However, when the device is turned into landscape mode, the right 40% of the screen is black (i.e. the Background is not drawn on the part of the screen that was not previously drawn). I do not know how to get a background for redrawing to fill the entire screen.

Can someone tell me what I am doing wrong? Thank!

Here's what it looks like when initially drawn. enter image description here

And now it looks like when I turn it:

enter image description here

Here is the code for my background gradient class:

#import "BackgroundGradient.h"

@interface BackgroundGradient()
@property (strong, nonatomic) UIView *view;
@end

@implementation BackgroundGradient

- (id) initWithView:(UIView *) view
{
    if (!_view)
        _view = view;

    return self;
}

- (void) makeGradient
{
    UIColor *darkOp = [UIColor colorWithRed:(193.0 / 255.0) green:(215.0 / 255.0) blue:(46.0 / 255.0) alpha: 1];
    UIColor *lightOp = [UIColor colorWithRed:(222.0 / 255.0) green:(233.0 / 255.0) blue:(143.0 / 255.0) alpha: 1];

    // Create the gradient
    CAGradientLayer *gradient = [CAGradientLayer layer];

    // Set colors
    gradient.colors = [NSArray arrayWithObjects:
                       (id)darkOp.CGColor,
                       (id)lightOp.CGColor,
                       (id)darkOp.CGColor,
                       nil];

    // Update the start and end points
    gradient.startPoint = CGPointMake(0.5, 0.0);
    gradient.endPoint = CGPointMake(0.5, 1.0);

    // Set bounds
    gradient.frame = self.view.bounds;

    // Add the gradient to the view
    [self.view.layer insertSublayer:gradient atIndex:0];

    gradient.locations = [NSArray arrayWithObjects:
                          [NSNumber numberWithFloat:0.0f],
                          [NSNumber numberWithFloat:0.7],
                          [NSNumber numberWithFloat:0.9],
                          nil];

}

@end

And finally, this is how I create the background object:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.background = [[BackgroundGradient alloc] initWithView:self.view];
    [self.background makeGradient];

}

EDIT: I added the following code and enabled updates for start and end points (above):

- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [self.background makeGradient];
}

, , . , - . , , . , .

enter image description here

+3
3

: CAGradientLayer *gradient = [CAGradientLayer layer]; [self.view.layer insertSublayer:gradient atIndex:0];

, . . , . . viewDidLoad

, .

+2

: .

Swift

override func willAnimateRotationToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
    self.backgroundImageView.layer.sublayers?.first?.frame = self.view.bounds

}
+3

Update Gradient Frame to viewDidLayoutSubviews:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    // find gradient layer
    let gradLayers = gradientView.layer.sublayers?.flatMap { $0 as? CAGradientLayer }
    // this assumes there is only one gradient layer
    gradLayers?.first?.frame = gradientView.bounds
}
+1
source

Source: https://habr.com/ru/post/1658937/


All Articles