gpt4 book ai didi

iOS开发 全机型适配解决方法

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 27 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章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.

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

原文链接:http://www.jianshu.com/p/09bee6c23272 。

最后此篇关于iOS开发 全机型适配解决方法的文章就讲到这里了,如果你想了解更多关于iOS开发 全机型适配解决方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com