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