Get the App
SLTechnology News&Howtos  ›  Development  › 

How to adapt through frame and AutoLayout

Shulou Source: shulou.com Published: 2022-06-02 04:36:15 09月16日 Update

This article mainly introduces "how to adapt through frame and AutoLayout". In daily operation, I believe many people have doubts about how to adapt through frame and AutoLayout. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to adapt through frame and AutoLayout"! Next, please follow the editor to study!

The purpose of screen adaptation

Purpose 1:

Different models of iPhone and iPad may have different screen sizes (different point coordinate systems)

When releasing an App, it should not only be aimed at one screen size, but should be able to run on devices of different screen sizes.

Purpose two:

IPhone supports rotation in three directions, while iPad supports rotation in four directions

When the screen rotates, the point coordinate system changes (the width and height of the screen changes), and the UI elements on the screen need to be rearranged.

Whether the App setting supports multiple directions:

In the project profile:

The controller overrides the supportedInterfaceOrientations method to specify the direction supported by the page

-(UIInterfaceOrientationMask) supportedInterfaceOrientations

The enumerated value of the supported direction is returned

By default, the horizontal screen does not display the status bar. If you need to display it, set it in Info.plist:

Add code to AppDelegate:

[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone]; [UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone]

Screen adaptation through frame

For purpose 1: when setting the child view frame, the values are calculated based on the width and height of the parent view

For purpose 2: when the screen is flipped, rewrite to calculate the width and height of one side of the sub-view

In the controller subclass, place the settings of the subview frame in the overridden viewDidLayoutSubviews method:

-(void) viewDidLayoutSubviews / / Note: the parent method before override should be called first in this method

In the view subclass, place the settings of the subview frame in the override layoutSubviews method:

-(void) layoutSubviews / / Note: the parent method before override should be called first in this method

Both methods are called automatically when the view of the current controller or the frame of the current view gets the initial value and changes.

For example, the btn button in the controller is centered and the length and width are specified to be 100.

-(void) viewDidLayoutSubviews {[super viewDidLayoutSubiews]; CGFloat x, y, w, h; w = h = 100; x = self.view.frame.size.width/2-w hand 2; y = self.view.frame.size.height/2-h hand 2; self.btn.frame = CGRectMake (x, y, w, h);}

AutoResizing

Used to determine the position of a view control on the entire screen and the effect when the screen size changes or reverses

You can ensure that the relative relationship of some or all of the frame properties of a view control does not change, resulting in effects such as centering, stretching and so on.

If you choose to use AutoLayout, you cannot use AutoResizing

Use in the code:

The UIView object sets stretchable properties through the method atuoResizingMask property

@ property (nonatomic) UIViewAutoresizing autoresizingMaskenum {UIViewAutoresizingNone = 0, UIViewAutoresizingFlexibleLeftMargin = 1

Tags: Screen method view adaptation property direction purpose support controller learning control difference effect change code coordinate coordinate system multiple size subclass Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Docker Huawei Shulou Tech Info Xiaomi