天天看点

iphone4、4S程序如何兼容Iphone5 (关键字 NSLayout、NSLayoutConstraint)

1.首先记录如何让程序兼容iphone5,很容易搜索到,此处简单记录。

1.xib中use Autolayout勾上 2.xib中Size--Retina 4 Full Screen 3.代码中判断是否是iphone5:---- #define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO) 代码控制,如:

if(iPhone5)

    {

    }else{

         CGRect rect = tabControllerHome.view.frame;

        rect.size.height = rect.size.height +88;

        tabControllerHome.view.frame = rect;

    }

2.其次,也是重点是XIB布局问题,Layout和NSLayoutConstraint

由于多控件的屏幕分辨率转成“Retina 4 Full Screen”的屏幕分辨率,IOS6会有自动的Constraints。可能有的控件可能达不到效果,比如动态数据的ScrollView,动态高度的ImageView。此处需要学习Layout的Constraint入门,下面有两篇很好的(Layout)入门文章:

http://www.raywenderlich.com/zh-hans/22873/ios-6-自动布局-入门-1

http://www.raywenderlich.com/zh-hans/23026/ios-6-自动布局-入门-2

如果仔细阅读上面文章,按照它修改XIB在布局中的位置或高度等还会有code无法控制UIController的情况出现,

首先检查XIB中的Constraints是否与code冲突,会动态改变的控件尽量不要写死,根据需求摆放控件在布局中的位置,

其次,如果控件的constraint在code中依旧无法控制动态修改,可以不用惊慌,因为还有NSLayoutConstraint类,下面亮出写法如:

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.imageViewPanel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:imageViewPanel.frame.size.height]];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.label attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeTop multiplier:1.0 constant:label.frame.origin.y]];

以此能达到动态显示控件的效果