1. 程式人生 > >iOS7 和iOS6的螢幕適配

iOS7 和iOS6的螢幕適配

//目的:用於iOS7和iOS6適配,和保留之前的座標編碼習慣,不用刻意加減
 
/*方法1:
 *1。在vc中重寫viewDidLayoutSubviews方法
 *2。是用下面2個方法之一;
 *3。frame為ios6風格,狀態列和導航欄為平鋪
 *4。bounds為ios7風格,狀態列和導航欄為覆蓋
 *5。優點,所有subview的座標都一ios6的標準進行編寫,支援push和present
 *6。缺點,在push中每個vc都需要重寫viewDidLayoutSubviews方法;
 */
void IOS7ToIOS6ofFrame(UIViewController *vc);
void IOS7ToIOS6ofBounds(UIViewController *vc);
 
void IOS7ToIOS6ofFrame(UIViewController *vc)
{
    if (IsIOS7) {
        CGRect rect = vc.view.frame;
        vc.view.frame = CGRectMake((rect.origin.x), (rect.origin.y+(IsIOS7?vc.topLayoutGuide.length:0)), (CGRectGetWidth(rect)), (CGRectGetHeight(rect)-(IsIOS7?vc.topLayoutGuide.length:0)));
    }
}
 
void IOS7ToIOS6ofBounds(UIViewController *vc)
{
    if (IsIOS7) {   
        CGRect rect = vc.view.bounds;
        if (rect.origin.y != -1*vc.topLayoutGuide.length) {
            vc.view.bounds = CGRectMake((rect.origin.x), (rect.origin.y+(IsIOS7?vc.topLayoutGuide.length*(-1):0)), (CGRectGetWidth(rect)), (CGRectGetHeight(rect)));
        }
    }
}
 
/*方法2:
 *1.在vc的init或viewdidload中使用IOS7巨集即可;
 *2.狀態列和導航欄為平鋪
 *3.優點:所有subview的座標都一ios6的標準進行編寫,支援push,背景frame值同ios6
 *4.缺點:在push中每個vc都需要寫,不支援present;
 */
 
#define IOS7 if([[[[UIDevice currentDevice] systemVersion] substringToIndex:1] intValue]>=7)\
{self.extendedLayoutIncludesOpaqueBars = NO;\
self.modalPresentationCapturesStatusBarAppearance =NO;\
self.edgesForExtendedLayout = UIRectEdgeNone;}
 
/*方法3:
 *1.使用self.navigationController.navigationBar.translucent =NO;
 *2.狀態列和導航欄為平鋪
 *3.優點:所有subview的座標都一ios6的標準進行編寫,支援push,背景frame值同ios6,只需要設定一次
 *4.缺點:必須有nav;對於present的vc必須為nav
 */
 
/*方法4:
 *1.重新定義CGRECT;
 *2.狀態列和導航欄為覆蓋
 *3.優點:隨時可以用
 */
#define IsIOS7 ([[[[UIDevice currentDevice] systemVersion] substringToIndex:1] intValue]>=7)
#define CGRECT_NO_NAV(x,y,w,h) CGRectMake((x), (y+(IsIOS7?20:0)), (w), (h))
#define CGRECT_HAVE_NAV(x,y,w,h) CGRectMake((x), (y+(IsIOS7?64:0)), (w), (h))