iOS判断用户当前设备型号iPhone-model

原文链接:http://www.2cto.com/kf/201504/395081.html

判断方法:
 
我们在适配多个机型时,大多情况下都需要考虑到用户设备的型号,然后根据用户设备的width,height,分辨率等来决定控件或图片的大小。那么如何获知用户设备的型号呢?
 
我个人是通过(下面这个方法)  
 
1
[[UIScreen mainScreen] bounds];
 来获取主屏幕的bounds,熟悉的朋友一看到bound或frame肯定就会想到CGRect 这个结构体(struct),而bounds当然也是这个结构体的一个变量
 
其中CGRect的成员中有一个CGSize(也是结构体),CGSize的成员包括width,height
 
所以当前我们就获得主屏幕的宽以及高了,然后对应下边的数据就可知道用户使用的具体机型了
 
如下:各系列机型竖屏时的 宽*高
 
portrait   width * height
 
iPhone4:320*480
 
iPhone5:320*568
 
iPhone6:375*667
 
iPhone6Plus:414*736
 
数据参考于   IOS设备设计完整指南  一文
 
 代码示例:
 
此处创建了一个UIDevice的category
 
UIDevice+IPhoneModel.h
 
 
 1 typedef NS_ENUM(char, iPhoneModel){//0~3
 2     iPhone4,//320*480
 3     iPhone5,//320*568
 4     iPhone6,//375*667
 5     iPhone6Plus,//414*736
 6     UnKnown
 7 };
 8 
 9 @interface UIDevice (IPhoneModel)
10 
11 /**
12  *  return current running iPhone model
13  *
14  *  @return iPhone model
15  */
16 + (iPhoneModel)iPhonesModel;
17 
18 @end
 
 
 
UIDevice+IPhoneModel.m
 
 
 1 #import "UIDevice+IPhoneModel.h"
 2 
 3 @implementation UIDevice (IPhoneModel)
 4 
 5 /**
 6  *  return current running iPhone model
 7  *
 8  *  @return iPhone model
 9  */
10 + (iPhoneModel)iPhonesModel {
11     //bounds method gets the points not the pixels!!!
12     CGRect rect = [[UIScreen mainScreen] bounds];
13 
14     CGFloat width = rect.size.width;
15     CGFloat height = rect.size.height;
16     
17     //get current interface Orientation
18     UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
19     //unknown
20     if (UIInterfaceOrientationUnknown == orientation) {
21         return UnKnown;
22     }
23     
24     //    portrait   width * height
25     //    iPhone4:320*480
26     //    iPhone5:320*568
27     //    iPhone6:375*667
28     //    iPhone6Plus:414*736
29     
30     //portrait
31     if (UIInterfaceOrientationPortrait == orientation) {
32         if (width ==  320.0f) {
33             if (height == 480.0f) {
34                 return iPhone4;
35             } else {
36                 return iPhone5;
37             }
38         } else if (width == 375.0f) {
39             return iPhone6;
40         } else if (width == 414.0f) {
41             return iPhone6Plus;
42         }
43     } else if (UIInterfaceOrientationLandscapeLeft == orientation || UIInterfaceOrientationLandscapeRight == orientation) {//landscape
44         if (height == 320.0) {
45             if (width == 480.0f) {
46                 return iPhone4;
47             } else {
48                 return iPhone5;
49             }
50         } else if (height == 375.0f) {
51             return iPhone6;
52         } else if (height == 414.0f) {
53             return iPhone6Plus;
54         }
55     }
56     
57     return UnKnown;
58 }
59 
60 @end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值