1.xcode工程
当旧工程没有任何改变是,旧App能被自动放大以便适应宽屏,此时屏幕分辨率还是(640x960) 。
当在xcode工程配置文件(工程名.plist)文件中添加入 “Launch screen interface file base name” 项,内容为你定义的“Launch Screen.xib” ,
(如下图所示)。
这个文件在xcode 6及以上版本 已经成为新建工程是自动生成的文件,这里注意:xcode 6以后新建的.xib文件都是自带 "constraints" 即 AutoLayout 布局(autoLayout 从ios6开始支持),如需兼容老版本要,去掉AutoLayout,只要将.xib属性中"use auto layout" 去掉勾选即可。
2.代码层面
有二种实现方式:
1. auotresizingMask
2. autoLayout (iOS6以上的才支持)
分析两种方式的优劣:
autoresizingMask 优:不用考虑版本兼容的问题,它应该是1.0就已经支持了。
劣:不灵活,复杂的界面不易处理。
autoLayout 优:用法灵活,可以应对复杂的界面(仿 android 布局方式),并为官方推荐,估计以后ios设备也会出越来越多的分辨率
劣:不支持ios6 以下的系统
auotresizingMask方式
可以通过.xib的属性为实现(如下图),也可在代码中设置 如下: [contentLabel setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
UIViewAutoresizingFlexibleLeftMargin :自动灵活调整左侧间距,即右侧的间距等比(靠右对齐)
UIViewAutoresizingFlexibleRightMargin :自动灵活调整右侧间距,即左侧的间距等比(靠左对齐)
UIViewAutoresizingFlexibleTopMargin :同理(靠下对齐)
UIViewAutoresizingFlexibleBottomMargin :同理(靠上对齐)
AutoLayout 方式:
有两种实现方式,1 直接一个一个“约束”的创建,然后用“addConstraint” 添加单个
2 通过 format 方式 ,返回一个“约束”列表,然后 “addConstraints” 添加一个约束队列
殊途同归,两种添加约束的效果都一样,当然也可以混合使用。直接上代码如下:
<pre name="code" class="objc"> //版本大于6.0用AutoLayout处理
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6)
{
[button setTranslatesAutoresizingMaskIntoConstraints:NO];
NSDictionary *views=NSDictionaryOfVariableBindings(button);
NSMutableArray* tmpConstraint = [NSMutableArray array];
[tmpConstraint addObject:[NSLayoutConstraint constraintWithItem:button
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0]];
[tmpConstraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[button(220)]"
options:0
metrics:nil
views:views]];
[tmpConstraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|-(%f)-[button]-|",button.frame.origin.y]
options:0
metrics:nil
views:views]];
[self addConstraints:tmpConstraint];
}
注意:以下代码
[button setTranslatesAutoresizingMaskIntoConstraints:NO];
当要对某个对象启用 AutoLayout时,要关闭autoresizingMask。
具体 fromat的格式参数,有时间再佃聊
适配第一阶段就先到这里,欢迎留言与探讨