我是靠谱客的博主 帅气航空,这篇文章主要介绍iOS开发 全机型适配解决方法,现在分享给大家,希望可以做个参考。

最近做项目,对于IPhone 手机机型适配很是头疼,所以整理下网上资料,记录下来,也许能帮助到正看文章的你,

今天打算跟大家聊聊最近研究的全机型适配思路。

当前我们需要适配的iPhone机型有4s、5s、6s、6Plus四种机型。它们的尺寸分别是

 iphone4s {320, 480}                           960*640
 iphone5 5s {320, 568}                       1136*640
 iphone6 6s   {375, 667}                     1334*750
 iphone6Plus 6sPlus {414, 736}         1920*1080

而一般我习惯在实际的项目开发中,使用Masonary来搭建UI界面,虽然在Masonary中我们能很方便的设置各个控件之间的约束,但是对于类似4s机型和6s Plus机型的很大的高度差,有时候仅仅靠一次性成型的约束还是搭建不出很合理的界面。

于是在这次搭建UI的过程中,我的一个思路就是按照比例,针对各个机型进行微调。思路如下:

美工提供的效果图是基于iPhone6的效果图

而我只需要将标注上的每个尺寸去对比iPhone6换算出比例,这样一些间距就能按照不同机型尺寸的比例变得不一样。

针对考虑交互体验的控件,在保持尺寸不变的基础上,做细节微调。
在具体的代码中,我封装出了一个类,定义了两个类方法专门去适配所有机型的高度和宽度。思路就是上述按不同机型针对于iPhone6的比例而适配。

代码我也贴一部分出来。

头文件的定义

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> typedef NS_ENUM(NSInteger, IPhoneType) { iPhone4Type = 0, iPhone5Type, iPhone6Type, iPhone6PlusType }; @interface CalculateLayout : NSObject /** * 基于UI设计的iPhone6设计图的全机型高度适配 * * @param height View高度 * * @return 适配后的高度 */ + (CGFloat)neu_layoutForAlliPhoneHeight:(CGFloat)height; /** * 基于UI设计的iPhone6设计图的全机型宽度适配 * * @param width 宽度 * * @return 适配后的宽度 */ + (CGFloat)neu_layoutForAlliPhoneWidth:(CGFloat)width;

.m文件的部分如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#define iPhone4Height (480.f) #define iPhone4Width (320.f) #define iPhone5Height (568.f) #define iPhone5Width (320.f) #define iPhone6Height (667.f) #define iPhone6Width (375.f) #define iPhone6PlusHeight (736.f) #define iPhone6PlusWidth (414.f) #pragma mark - 适配所有机型高度 + (CGFloat)neu_layoutForAlliPhoneHeight:(CGFloat)height { CGFloat layoutHeight = 0.0f; if (iPhone4) { layoutHeight = ( height / iPhone6Height ) * iPhone4Height; } else if (iPhone5) { layoutHeight = ( height / iPhone6Height ) * iPhone5Height; } else if (iPhone6) { layoutHeight = ( height / iPhone6Height ) * iPhone6Height; } else if (iPhone6P) { layoutHeight = ( height / iPhone6Height ) * iPhone6PlusHeight; } else { layoutHeight = height; } return layoutHeight; } + (CGFloat)neu_layoutForAlliPhoneWidth:(CGFloat)width { CGFloat layoutWidth = 0.0f; if (iPhone4) { layoutWidth = ( width / iPhone6Width ) * iPhone4Width; } else if (iPhone5) { layoutWidth = ( width / iPhone6Width ) * iPhone5Width; } else if (iPhone6) { layoutWidth = ( width / iPhone6Width ) * iPhone6Width; } else if (iPhone6P) { layoutWidth = ( width / iPhone6Width ) * iPhone6PlusWidth; } return layoutWidth; }

代码我也已经放在了Github上,如果这些对你有帮助,在clone代码之余能否给个star。

感谢阅读,希望能帮助到大家,谢谢大家对本站点的支持!

最后

以上就是帅气航空最近收集整理的关于iOS开发 全机型适配解决方法的全部内容,更多相关iOS开发内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(97)

评论列表共有 0 条评论

立即
投稿
返回
顶部