接手公司項(xiàng)目,在里面看見(jiàn)了屏幕適配的方法祟敛,感覺(jué)很方便馆铁,缺點(diǎn)就是只支持豎屏埠巨。
1.首先在工程里面創(chuàng)建一個(gè)pch文件。這個(gè)百度一下就行侥猬,網(wǎng)址http://www.reibang.com/p/67ce72c4ad6c瞧预。
2.再來(lái)到appDelegate.h里面生成兩個(gè)屬性玖瘸。
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (assign, nonatomic) float autoSizeScaleX;
@property (assign, nonatomic) float autoSizeScaleY;
@end
3.來(lái)到appDelegate.m里面凿将,先將獲得屏幕的寬高寫(xiě)個(gè)宏定義,再在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法里面添加下面代碼:
#define ScreenHeight [[UIScreen mainScreen] bounds].size.height
#define ScreenWidth [[UIScreen mainScreen] bounds].size.width
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
if(ScreenHeight > 480 && ScreenHeight<1024){
myDelegate.autoSizeScaleX = ScreenWidth/320;
myDelegate.autoSizeScaleY = ScreenHeight/568;
}else if(ScreenHeight >= 1024){
myDelegate.autoSizeScaleX = ScreenWidth/320;
myDelegate.autoSizeScaleY = ScreenHeight*1.333333/568;
}else
{
myDelegate.autoSizeScaleX = 1.0;
myDelegate.autoSizeScaleY = 1.0;
}
return YES;
}
4.在pch文件里面添加AppDelegate的頭文件
#ifndef PrefixHeader_pch
#define PrefixHeader_pch
#import "AppDelegate.h"
#endif /* PrefixHeader_pch */
5.創(chuàng)建一個(gè)Header file文件如下圖
6.在Header file文件里面添加下面代碼
#ifndef SetFrame_h
#define SetFrame_h
CG_INLINE CGRect CGRectMakes(CGFloat x, CGFloat y, CGFloat width, CGFloat height)
{
AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
CGRect rect;
rect.origin.x = x * myDelegate.autoSizeScaleX;
rect.origin.y = y * myDelegate.autoSizeScaleY;
rect.size.width = width * myDelegate.autoSizeScaleX;
rect.size.height = height * myDelegate.autoSizeScaleY;
return rect;
}
CG_INLINE CGSize CGSizeMakes(CGFloat width, CGFloat height)
{
AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
CGSize rect;
rect.width = width * myDelegate.autoSizeScaleX;
rect.height = height * myDelegate.autoSizeScaleY;
return rect;
}
CG_INLINE CGPoint CGPointMakes(CGFloat width, CGFloat height){
AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
CGPoint rect;
rect.x = width * myDelegate.autoSizeScaleX;
rect.y = height * myDelegate.autoSizeScaleY;
return rect;
}
CG_INLINE CGFloat TableCellHeight(CGFloat height){
AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
CGFloat newHeight;
newHeight = height * myDelegate.autoSizeScaleY;
return newHeight;
}
#endif /* SetFrame_h */
7.在pch文件里面添加Header file的頭文件
#ifndef PrefixHeader_pch
#define PrefixHeader_pch
#import "AppDelegate.h"
#import "SetFrame.h"
#endif /* PrefixHeader_pch */
這樣就可以使用了价脾,有4個(gè)方法可以調(diào)用牧抵,分別是CGRectMakes(CGFloat x, CGFloat y, CGFloat width, CGFloat height)、CGSizeMakes(CGFloat width, CGFloat height)侨把、CGPointMakes(CGFloat width, CGFloat height)和TableCellHeight(CGFloat height)犀变。使用的時(shí)候直接將系統(tǒng)的修改成這是個(gè)方法,比如創(chuàng)建一個(gè)label秋柄,直接將系統(tǒng)的CGRectMake改為CGRectMakes就可以了获枝。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMakes(10, 10,10 , 10)];
label.backgroundColor = [UIColor redColor];
[self.view addSubview:label];
}