接手公司项目,在里面看见了屏幕适配的方法,感觉很方便,缺点就是只支持竖屏。
1.首先在工程里面创建一个pch文件。这个百度一下就行,网址http://www.jianshu.com/p/67ce72c4ad6c。
2.再来到appDelegate.h里面生成两个属性。
#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.来到appDelegate.m里面,先将获得屏幕的宽高写个宏定义,再在- (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.创建一个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个方法可以调用,分别是CGRectMakes(CGFloat x, CGFloat y, CGFloat width, CGFloat height)、CGSizeMakes(CGFloat width, CGFloat height)、CGPointMakes(CGFloat width, CGFloat height)和TableCellHeight(CGFloat height)。使用的时候直接将系统的修改成这是个方法,比如创建一个label,直接将系统的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];
}