iOS 适配

  先下一个定义,这里我们把实现控件在不同版本、不同设备以及横竖屏状态下都能不冲突得展示称之为适配。能满足已有的三者之间任何一个组合情况,是我们做适配的最终目的。

  •   不同版本(Version)

  这里我们需要了解Xcode开发中需要设置的两个个值:BaseSDKDeploymentTarget

  Base SDK 指的是,当前编译所用的SDK 版本; OS Deployment Target  指的是,编译后的 app 可在 终端的哪个 版本上运行。

  iPhone OS的版本众多,很多用户由于各种各样的原因没有升级到最新版,这就给我们开发者带了麻烦。作为开发者,我们都希望软件的受众越多越好。怎么样让软件尽量适应最多的iPhone OS?Base SDK设置为当前xcode所支持的最高的sdk版本,比如"iphone SDK 8.4"。iPhone OS Deployment Target设置为你所支持的最低的iPhone OS版本,比如"iPhone OS 6.0"。

  其中,Base SDK的版本对于适配的影响是系统级的,不同版本所支持的方法有个别的差异,如果不能进行有效判断酒会造成界面布局失调、程序报错甚至崩溃。具体例子如下:

  edgesForExtendedLayout   是一个类型为UIExtendedEdge的属性,指定边缘要延伸的方向。

因为iOS7鼓励全屏布局,它的默认值很自然地是UIRectEdgeAll,四周边缘均延伸,就是说,如果即使视图中上有navigationBar,下有tabBar,那么视图仍会延伸覆盖到四周的区域。

   self.edgesForExtendedLayout = UIRectEdgeNone; 

来解决UINavigationBar透明的问题。设置了UIRectEdgeNone之后,你嵌在UIViewController里面的UITableView和UIScrollView就不会穿过UINavigationBar了,同时UIView的控件也回复到了iOS6时代。

  设置导航栏背景颜色也因版本而异:

  if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {

        [self.navigationController.navigationBar setBarTintColor:[UIColor greenColor]]; //iOS 7中,该方法同时设置导航栏和状态栏的背景颜色

    } else {

//        [[UINavigationBar appearance] setTintColor:[UIColor greenColor]];    //iOS 6 中两种设置方法都可以,不过,状态栏和导航栏不是一块

        [self.navigationController.navigationBar setTintColor:[UIColor greenColor]];

    }

  文本框根据文字内容适配大小的方法也有区别    

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {

        boundingRectWithSize: options: attributes: context:

    } else {

    sizeWithFont: constrainedToSize: lineBreakMode:

    }

// 判断是否有调用相机的权限(该属性只在iOS 7之后才有)

    if (currentSystemVersionIsIOS7) {

        AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];

        if (authStatus == AVAuthorizationStatusRestricted ||authStatus ==AVAuthorizationStatusDenied){

            

            [[[UIAlertView alloc] initWithTitle:@"开启相机访问权限" message:@"请进入手机『设置->隐私->相机』,开启『2345浏览器』选项开关" delegate:self cancelButtonTitle:@"立即开启" otherButtonTitles:nil]show];

            

        }

    }

  同时,需要注意添加视图的方式,如果是通过  [[[UIApplication sharedApplication].windows firstObject] addSubview: someView]  方式来添加的,该someView视图是在添加在窗口上的,其frame相当于UIRectEdgeAll,与版本无关。

   UIActionSheet在iOS 6的设备上横屏也能适配到充满屏幕宽度,而在iOS 7后不能改变宽度。在iOS 8中,用UIAlertController以及UIAlertAction的组合代替了 UIAlertViewUIActionSheet

 

  •  不同设备simulator(模拟器设备)

    simulator就是现实中我们用到的设备,常见的就是iphone 4(s)、iphone 5(s)、iphone 6(plus)。

    

设备

iPhone

Width

Height

对角线

Diagonal

逻辑分辨率(point)

Scale Factor

设备分辨率(pixel)

PPI

3GS

2.4 inches (62.1 mm)

4.5 inches (115.5 mm)

3.5-inch

 

320x480

@1x

320x480

163

4(s)

2.31 inches (58.6 mm)

4.5 inches (115.2 mm)

3.5-inch

320x480

@2x

640x960

326

5c

2.33 inches (59.2 mm)

4.90 inches (124.4 mm)

4-inch

320x568

@2x

640x1136

326

5(s)

2.31 inches (58.6 mm)

4.87 inches (123.8 mm)

4-inch

320x568

@2x

640x1136

326

6

2.64 inches (67.0 mm)

5.44 inches (138.1 mm)

4.7-inch

375x667

@2x

750x1334

326

6+

3.06 inches (77.8 mm)

6.22 inches (158.1 mm)

5.5-inch

414x736

@3x

(1242x2208->)

1080x1920

401 

   

 

 

 

 

 

 

 

 

原文地址:https://www.cnblogs.com/Jacue/p/4719353.html