IOS screen Adaptation

Source: Internet
Author: User

For iOS development, screen adaptation is really important, here is not much to say, today mainly to introduce you to proportional adaptation;

1. First we define two macro definitions in the AppDelegate.h, the width and height of the screen

#import <UIKit/UIKit.h>

Macro definition

#define ScreenHeight [[UIScreen Mainscreen] Bounds].size.height

#define SCREENWIDTH [[UIScreen Mainscreen] Bounds].size.width

@interface Appdelegate:uiresponder <UIApplicationDelegate>

@property (Strong, nonatomic) UIWindow *window;

Set the scale of auto-scaling

@property (nonatomic, assign) float Autosizescalex;

@property (nonatomic, assign) float Autosizescaley;

@end

2. In the APPDELEGATE.M file, a multiple of the calculation Zoom screen is implemented:

#import "AppDelegate.h"

@interface Appdelegate ()

@end

@implementation Appdelegate

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {

Override point for customization after application launch.

Create a uiapplication singleton and specify the proxy so that you can pass the value

Appdelegate *mydelegate = [[UIApplication sharedapplication] delegate];

Judging the height of the screen if it is greater than 480, note that the 5s is the benchmark

if (ScreenHeight >480) {

Calculate the multiples of auto-scaling

Mydelegate.autosizescalex = screenwidth/320;

Mydelegate.autosizescaley = screenheight/568;

}

else{

Less than 480 is iPhoneon4, so the normal display

Mydelegate.autosizescalex = 1.0;

Mydelegate.autosizescaley = 1.0;

}

return YES;

}

3. Add a cg_inline cgrect (inline function) to the page where you want the screen to fit:

#import "ViewController.h"

#import "AppDelegate.h"

@interface Viewcontroller ()

@end

@implementation Viewcontroller

-(void) Viewdidload {

[Super Viewdidload];

}

-(void) didreceivememorywarning {

[Super didreceivememorywarning];

Dispose of any resources the can be recreated.

}

inline functions

Cg_inline CGRect

Redefining the dimensions of a control

CGRentMake1 (CGFloat x, cgfloat y, cgfloat width, cgfloat height) {

CGRect rect;

Appdelegate *myappdelegate = [[UIApplication sharedapplication] delegate];

Proportionally scaled coordinates, and dimensions

rect.origin.x = X*myappdelegate.autosizescalex;

RECT.ORIGIN.Y = Y*myappdelegate.autosizescaley;

Rect.size.width = Width*myappdelegate.autosizescalex;

Rect.size.height = Width*myappdelegate.autosizescaley;

return rect;

}

@end

4. After writing the inline function, the frame of the control you define later do not use CGRectMake to set the size, with the CGRentMake1 to redefine the control to set , so that the automatic scaling can be self-willed in

The iphone is available in any size. Give it a try!

Note: This method is only suitable for applications where precision is not required;

IOS screen Adaptation

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.