<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">在网上找了些屏幕适配的方法,因为之前的适配把屏幕划分为不同比例的部分,还有就是使用 storyBoard ,由于是纯代码开发,又是在基本功能都完成了之后 才适配,就想着找一些简单的适配方法</span>
手机型号 | 宽 | 长 |
iPhone 4s | 320 | 480 |
iPhone 5 | 320 | 568 |
iPhone 5s | 320 | 568 |
iPhone 6 | 375 | 667 |
iPhone 6 plus | 414 | 736 |
iPhone 6s | 375 | 667 |
iPhone 6s plus | 414 | 736 |
每个屏幕之间都是有一定的比例的,如果你做好了一个型号的话,想让其他型号显示的效果也和这个做好的型号的效果相同,那么就可以用这个方法:通过计算各个尺寸手机的比例 重新定义 CGRectMake 和 CGPointMake 方法,那么效果是一样的
1, 在 AppDelegate.h 文件中 声明:
@property float autoSizeScaleX;
@property float autoSizeScaleY;
2, 在 AppDelegate.m 文件中 写如下方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
if (kScreenHeight != 667) {
myDelegate.autoSizeScaleX = kScreenWidth/375;
myDelegate.autoSizeScaleY = kScreenHeight/667;
} else {
myDelegate.autoSizeScaleX = 1.0;
myDelegate.autoSizeScaleY = 1.0;
}
return YES;
}
导入 AppDelegate.h
然后在 文件最下方 写重构 CGRectMake 和 CGPointMake 方法
CG_INLINE CGRect
CGRectMake1(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 CGPoint
CGPointMake1(CGFloat x, CGFloat y)
{
AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
CGPoint point;
point.x = x * myDelegate.autoSizeScaleX;
point.y = y * myDelegate.autoSizeScaleY;
return point;
}
在需要适配的控件 初始化的地方 ,把 CGRectMake 和 CGPointMake 换成 CGRectMake1 和 CGPointMake1,如:
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake1(0, 0, image.size.width/2, image.size.height/2)];
imgView.center = CGPointMake1(604/4, 174/2+image.size.height/2/2);