Iphone 6&6p,IOS8適配工作總結

來源:互聯網
上載者:User

標籤:ios   iphone 6   iphone 6p   適配   ios 8   

一、IOS8適配遇到的問題

1、不能定位
打勾 設定- 隱私-定位服務-你的app-使用應用程式期間(始終)
開啟app再進設定後會發現,你打勾的使用程式期間(始終)又給取消了

原來iOS8需要一些方法。
如果需要僅在前台定位,你在調用startUpdatingLocation 前需要調用requestWhenInUseAuthorization
如果需要在前後台定位,你在調用startUpdatingLocation 前需要調用requestAlwaysAuthorization
同 時在plist檔案中添加NSLocationWhenInUseUsageDescription或NSLocationAlwaysUsageDescription欄位,值寫"需要定位"就可以了,也可以是其他的,這個提示文字"需要定位"在詢問使用者授權的時候會顯示到的。

1.if ([[[UIDevice currentDevice] systemVersion] floatValue]>= 8.0) {

2.[_locationManager requestWhenInUseAuthorization];

3.}

4. 

5.[_locationManager startUpdatingLocation];


2、推送不管用

01.if ([[[UIDevice currentDevice] systemVersion] floatValue]>= 8.0) {

02.[app registerForRemoteNotifications];

03. 

04.UIUserNotificationSettings *settings =[UIUserNotificationSettings settingsForTypes:

05.UIRemoteNotificationTypeAlert

06.| UIRemoteNotificationTypeBadge

07.| UIRemoteNotificationTypeSoundcategories:nil];

08.[appregisterUserNotificationSettings:settings];

09. 

10.} else {

11.[app registerForRemoteNotificationTypes:

12.UIRemoteNotificationTypeAlert

13.| UIRemoteNotificationTypeBadge

14.| UIRemoteNotificationTypeSound];

15.}

ios8註冊推送分兩步走,

1》註冊使用者通知

UIUserNotificationSettings *settings =[UIUserNotificationSettings settingsForTypes:

UIRemoteNotificationTypeAlert

| UIRemoteNotificationTypeBadge

| UIRemoteNotificationTypeSound categories:nil];

[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

2》使用者通知註冊成功後,會走如下回調,在回呼函數中進行推送註冊:

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000

- (void)application:(UIApplication *)applicationdidRegisterUserNotificationSettings:(UIUserNotificationSettings*)notificationSettings

{

   [[UIApplication sharedApplication] registerForRemoteNotifications];

}

#endif

3》接下來就走以前的推送回調:

- (void)application:(UIApplication *)applicationdidRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken  //成功回調

- (void)application:(UIApplication*)applicationdidFailToRegisterForRemoteNotificationsWithError:(NSError*)error        //失敗回調

判斷推送開啟開關,ios8以前的代碼:

[[UIApplication sharedApplication]enabledRemoteNotificationTypes]!=UIRemoteNotificationTypeNone;

ios8需要換成如下代碼:

[[UIApplication sharedApplication]isRegisteredForRemoteNotifications]

3、某些系統控制項的布局計算延遲到繪製時才發生:

舉個例子:

UIButton * button = [UIButtonbuttonWithType:UIButtonTypeCustom];

        [buttonsetTag:i];

        [buttonsetTitle:siteData.info.title

               forState:UIControlStateNormal];

       [button.titleLabel setLineBreakMode:NSLineBreakByTruncatingTail];

        [buttonsetTitleColor:[UIColor colorWithHexValue:0x000000]

                    forState:UIControlStateNormal];

        [buttonsetTitleColor:[UIColor colorWithHexValue:0xffffff]

                    forState:UIControlStateHighlighted];

        [buttonsetTitleColor:[UIColor colorWithHexValue:0xffffff]

                    forState:UIControlStateSelected];

       [button.titleLabel setFont:[UIFont systemFontOfSize:14]];

        [button setBackgroundColor:[UIColorclearColor]];

       

        UIImage*image = [UIImage imageNamed:@"sourceSelect"];

        image =[image resizableImageWithCapInsets:UIEdgeInsetsMake(4, 2, 4, 2)];

        [buttonsetBackgroundImage:nil

                          forState:UIControlStateNormal];

        [buttonsetBackgroundImage:image

                         forState:UIControlStateHighlighted];

        [buttonsetBackgroundImage:image

                         forState:UIControlStateSelected];

       button.exclusiveTouch = YES;

       button.selected = NO;

        UIImage*tmpImg = [Utility ImageFromColor:[UIColor clearColor]

                                            rect:CGRectMake(0, 0, 47, 60)];

        [buttonsetImageWithURL:[NSURL URLWithString:siteData.info.logoURL]

              placeholderImage:tmpImg];

        //ios8layout會延遲到渲染的時候,這個地方需要算座標,所以需要手動強制layout

        [buttonsetNeedsLayout];

       [button layoutIfNeeded];

        NSIntegertitleWidth = CGRectGetWidth(button.titleLabel.frame);

4、某些系統控制項的動畫會有延遲現象:

例子:

       [UIViewbeginAnimations:nil context:nil];

       [UIViewsetAnimationDuration:duration];

       [progressBar setAlpha:alpha];

       progressBar.frame = frame;

[UIView commitAnimations];

以上代碼是瀏覽器進度條變化的動畫,在IOS7工作良好,在Ios8中卻會出現進度後退的奇怪現象。

5、UITableViewCell有一個預設size(320,44):

在- (id)initWithStyle:(UITableViewCellStyle)stylereuseIdentifier:(NSString *)reuseIdentifier方法中不要依賴於self.frame或者self.contentView.frame,因為這個時候的size都是預設值size(320,44).

有兩種解決方案,

1>>重寫setFrame方法,

-(void)setFrame:(CGRect)frame

{

   frame.size.width=VIEW_WIDTH;//VIEW_WIDTH這裡是螢幕豎屏時的寬

    [supersetFrame:frame];

}

2>>在- (id)initWithStyle:(UITableViewCellStyle)stylereuseIdentifier:(NSString *)reuseIdentifier方法中把寬寫死,不要寫成根據self.frame.width算出來的值,例如需要與螢幕等寬,用[UIScreen mainScreen].bounds.width,

 

二、Iphone6&6p適配遇到的問題

1、如何管理圖片,對於6p圖片加上@3x尾碼,系統即可自動為我們載入所需要的圖片,但是對於6的話,由於6和4/4s/5/5s的倍率是一樣的,如果想針對6單獨做圖片的話,系統就無法知道我們要載入的圖片是哪一個了。這個時候有兩種做法:

1》》做一個人為約定,例如針對6的圖片名字都加上“_6”尾碼,然後針對UIImage做個擴充:

例如:

+(instancetype)imageNamedForDevices:(NSString*)imageName

{

    NSRangeatPos=[imageName rangeOfString:@"@"];

    if(atPos.location!=NSNotFound) {

       imageName=[imageName substringToIndex:atPos.location];

    }

   UIImage* defaultImage=[UIImage imageNamed:imageName];

   UIImage* iphone6Image=defaultImage;

   if(![imageName hasSuffix:@"_6"])

    {

       iphone6Image=[UIImage imageNamed:[imageName stringByAppendingString:@"_6"]];

        if(!iphone6Image) {

           iphone6Image=defaultImage;

        }

    }

    returnVALUE_FOR_UNIVERSE_DEVICE(defaultImage, iphone6Image, defaultImage);

}

這樣的話,加入我們有個圖片有針對5s,6,6p有三個版本,分別命名為[email protected], [email protected], [email protected],

我們在程式中只要調用[UIImage imageNamedForDevices:@”home_bar”];就可以根據裝置擷取到對應版本的圖片。

2》》手動寫if else語句,根據裝置編寫不同的擷取圖片的代碼,不推薦這種做法。

 

2、如何針對特定裝置做定製化需求:

typedef enum

{

   iPhoneDeviceTypeIPhone4,

   iPhoneDeviceTypeIPhone4S=iPhoneDeviceTypeIPhone4,

    iPhoneDeviceTypeIPhone5,

   iPhoneDeviceTypeIPhone5S=iPhoneDeviceTypeIPhone5,

   iPhoneDeviceTypeIPhone6,

   iPhoneDeviceTypeIPhone6P

}iPhoneDeviceType;

iPhoneDeviceType global_deviceType;

#define IS_IPHONE_5OR_ABOVE   (global_deviceType>=iPhoneDeviceTypeIPhone5S)

#define IS_IPHONE_6P  (global_deviceType==iPhoneDeviceTypeIPhone6P)

#define IS_IPHONE_6OR_ABOVE  (global_deviceType>=iPhoneDeviceTypeIPhone6)

#define IS_IPHONE_6          (global_deviceType==iPhoneDeviceTypeIPhone6)

#define VALUE_FOR_UNIVERSE_DEVICE(a,b,c)  ((IS_IPHONE_6P)?(a):((IS_IPHONE_6)?(b):(c)))

//擷取裝置尺寸資訊

+(void)getDeviceType

{

   iPhoneDeviceType type;

    CGRectbounds=[[UIScreen mainScreen] bounds];

    CGFloatheight=bounds.size.height;

    CGFloatscale=[UIScreen mainScreen].scale;

    if(height<568) {

       type=iPhoneDeviceTypeIPhone4S;

    }

    elseif(height<667)

    {

       type=iPhoneDeviceTypeIPhone5S;

    }

    elseif(scale<2.9)

    {

       type=iPhoneDeviceTypeIPhone6;

    }

    else

    {

       type=iPhoneDeviceTypeIPhone6P;

    }

   global_deviceType=type;

}

VALUE_FOR_UNIVERSE_DEVICE這個宏能夠讓我們對裝置的判斷集中到一點,大大簡化了代碼,a,b,c可以是任意類型,只要保證a,b,c是同一類型即可,a是為6P定製的值,b是為6定製的值,c是為6以下設別定製的值。

3、涉及到橫豎屏切換的VC,代碼凡是用到UIScreen、UIWindow定位的地方都需要改寫,把width換成MIN(width,height),把height換成MAX(height,width).

4、座標依賴法則:1>>ViewController中的所有座標應該依賴於self.view.bounds;2>>UIView中的所有座標應該依賴於self.bounds;3>>其他情況的座標應該依賴於螢幕尺寸;1、2屬於相對座標,3屬於絕對座標。我們應該優先考慮相對座標,盡量少用絕對座標。

5、總體適配原則:1>>首先考慮橫向充滿,縱向根據需要增加高度;

                 2>>海報、大圖片、小窗播放器根絕橫向放大係數進行等比放大;

                 3>>海報的內邊距保持不變,海報與文字的距離保持不變,文字字型、文字上下之間的間距根據視覺需要做微調;

                 4>>對於那些沒有專門提供iPhone6&6p切圖的圖片,為了保證圖片不虛,需要把frame寫成與圖片尺寸一樣大。

6、關於適配解決方案的討論:

有三種方案:

1>>xib+autolayout;

這種方案是最為理想的適配方案,VC的座標體系一目瞭然,只需要簡單計算即可適配新裝置。前提是原始碼用到了xib來寫VC.

2>>手寫VC+手寫autolayout;

個人認為這種方案是最為繁瑣的方案,首先需要大量的座標計算,然後需要對VFL文法非常瞭解,解決約束依賴缺失或者衝突之類的問題,最後導致的結果是座標計算繁瑣複雜,代碼改動非常大,代碼增加量很大,不易於維護。

3>>手寫VC+手動適配;

這種方案也避免不了繁瑣的座標計算,但是相對2方案來講,不需要去瞭解VFL文法,也不需要解決約束依賴缺失或者衝突之類的問題,代碼改動量相對較少。我們工程中目前採用的就是這種方案。

Iphone 6&6p,IOS8適配工作總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.