Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to adapt through frame and AutoLayout

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report