Automatic Layout of IOS Layer & quot;

Source: Internet
Author: User

"Automatic Layout" of IOS Layer"

 

Today, when I answered a question to a foreign friend on stackoverflow, I encountered the problem of automatic Layer layout. Write it here and share it with the people you need.

Does Layer Support autolayout?

Currently, ios CALayer does not support AutoLayout or autoresizingMask.

For example

If you want to draw a gradient color as the background color. Define a configuration function

-(void)setupCAGradientLayer:(CAGradientLayer *)gradient{    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];    gradient.colors = colors;    gradient.locations = locations;}

If you do not use an image as the background, you can use a Layer to draw the image directly.
Easy to draw

#import ViewController.h@interface ViewController ()@property (strong,nonatomic) CAGradientLayer * gradient;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    self.gradient = [CAGradientLayer layer];    [self setupCAGradientLayer:self.gradient];    self.gradient.frame = self.view.frame;    [self.view.layer addSublayer:self.gradient];}


However, the screen is incorrect.

Rough Solution

The most intuitive solution is to adjust the layer size when the view bounds is changed.
The code is very simple. When the view bounds changes, the corresponding controller calls viewDidLayoutSubviews.
Therefore, add the following code to the viewController,

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

Normal horizontal screen

However, we all know that IOS will automatically generate an animation when switching between the horizontal and vertical screens. If a rough solution is used, the frame is changed after the animation ends, while the animation is still blank

Good Solution

Because IOS supports view well, it is convenient to use auto layout or autoresizingmask. Therefore, a better solution is to use view for processing.

Bind layer to view
Define a view and set its layer class to CAGradientClass.

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

Then, use this view to set autoresizingmask

#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];    [self setupCAGradientLayer:(CAGradientLayer*)self.backgroundview.layer];   }@end

Let's take a look at the switched animation (the gif is a bit watermark, sorry)

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.