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)

    {

        /*CGRect rect = tabControllerHome.view.frame;

        rect.origin.y = rect.origin.y - 88;

        tabControllerHome.view.frame = rect;*/

    }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]];


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






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值