iOS13 適配詳細(xì)

對(duì)iOS13 適配的總結(jié),下面有些是自己遇到的,有些是收集的,希望能給大家一些思路

  • iOS13中presentViewController的問(wèn)題
  • iOS不允許valueForKey、setValue: forKey獲取和設(shè)置私有屬性,需要使用其它方式修改
  • 黑線處理crash
  • 黑夜模式 審核強(qiáng)制要求適配黑夜模式。
  • iOS13上獲取DeviceToken
  • App啟動(dòng)過(guò)程中,部分View可能無(wú)法實(shí)時(shí)獲取到frame
  • 狀態(tài)欄(StatusBar)
  • Sign in with Apple -提供第三方登錄
  • 即將廢棄的 LaunchImage
  • MPMoviePlayerController 在iOS 13已經(jīng)不能用了
  • UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
  • UISegmentedControl 選中的顏色
  • 內(nèi)嵌WebView,一些圖片路徑,文件路徑,不用寫絕對(duì)路徑,直接寫文件名字即可,
  • 獲取網(wǎng)絡(luò)狀態(tài)用到KVC的那種方法會(huì)發(fā)生崩潰

  1. 在iOS13中運(yùn)行代碼發(fā)現(xiàn)presentViewController和之前彈出的樣式不一樣。

會(huì)出現(xiàn)這種情況是主要是因?yàn)槲覀冎皩?duì)UIViewController里面的一個(gè)屬性,即modalPresentationStyle(該屬性是控制器在模態(tài)視圖時(shí)將要使用的樣式)沒(méi)有設(shè)置需要的類型。在iOS13中modalPresentationStyle的默認(rèn)改為UIModalPresentationAutomatic,而在之前默認(rèn)是UIModalPresentationFullScreen。

ViewController *vc = [[ViewController alloc] init];
vc.title = @"presentVC";
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
nav.modalPresentationStyle = UIModalPresentationFullScreen;
[self.window.rootViewController presentViewController:nav animated:YES completion:nil];

注意:如果你某個(gè)控制器想使用卡片模式,需要注意你這個(gè)控制器底部是否有控件??ㄆ降牡撞康目丶菀妆徽趽酢1确秸f(shuō)TZPhotoPickerController 這個(gè)常用的開源相冊(cè)控件。當(dāng)選擇照片時(shí)底部的確定按鈕就被遮擋。無(wú)法選中

  1. 在使用iOS 13運(yùn)行項(xiàng)目時(shí)突然APP就crash掉了。定位到的問(wèn)題是在設(shè)置UITextField的Placeholder也就是占位文本的顏色和字體時(shí)使用了KVC的方法:
[_textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
[_textField setValue:[UIFont systemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];

可將其替換為

_textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"姓名" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:[UIColor redColor]}];

UIsearchBar 使用“ _searchField”崩潰修改方式

  UITextField *searchField;
    if (@available(iOS 13.0, *)) {
       searchField =[self.searchBar valueForKey:@"_searchTextField"];
      }else
      {
          searchField = [ self.searchBar valueForKey:@"_searchField"];
      }
  1. 之前為了處理搜索框的黑線問(wèn)題會(huì)遍歷后刪除UISearchBarBackground,在iOS13會(huì)導(dǎo)致UI渲染失敗crash;解決辦法是設(shè)置UISearchBarBackground的layer.contents為nil
public func clearBlackLine() {
        for view in self.subviews.last!.subviews {
            if view.isKind(of: NSClassFromString("UISearchBarBackground")!) {
                view.backgroundColor = UIColor.white
                view.layer.contents = nil
                break
            }
        }
    }
  1. 審核強(qiáng)制要求適配黑夜模式。
// 模式強(qiáng)制切換
if (darkMode) {
    if (@available(iOS 13.0, *)) {
        [UIApplication sharedApplication].keyWindow.overrideUserInterfaceStyle = UIUserInterfaceStyleDark;
    }
} else {
    if (@available(iOS 13.0, *)) {
        [UIApplication sharedApplication].keyWindow.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
    }
在 Info.plist 文件中,添加 key 為 User Interface Style,類型為 String,value 設(shè)置為 Light 可關(guān)閉黑暗模式。


已知問(wèn)題

  • YYLabel 如果使用了 textLayout屬性,切換模式的時(shí)候 無(wú)法自動(dòng)修改layout文本的顏色
  • 內(nèi)嵌WebView,需要手動(dòng)修改css樣式
  1. iOS13 正確的獲得Devicetoken的代碼如下:
#include <arpa/inet.h>

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    if (![deviceToken isKindOfClass:[NSData class]]) return;
    const unsigned *tokenBytes = (const unsigned *)[deviceToken bytes];
    NSString *hexToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
                          ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
                          ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
                          ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
    NSLog(@"deviceToken:%@",hexToken);
}

https://developer.umeng.com/docs/66632/detail/126489

  1. 可能是為了優(yōu)化啟動(dòng)速度,App 啟動(dòng)過(guò)程中,部分View可能無(wú)法實(shí)時(shí)獲取到正確的frame
  2. 狀態(tài)欄(StatusBar)

目前狀態(tài)欄也增加了一種模式,由之前的兩種,變成了三種, 其中default由之前的黑色內(nèi)容,變成了會(huì)根據(jù)系統(tǒng)模式,自動(dòng)選擇當(dāng)前展示lightContent還是darkContent。

public enum UIStatusBarStyle : Int {
  case `default` // Automatically chooses light or dark content based on the user interface style

  @available(iOS 7.0, *)
  case lightContent // Light content, for use on dark backgrounds

  @available(iOS 13.0, *)
  case darkContent // Dark content, for use on light backgrounds
}

我們?cè)谑褂玫臅r(shí)候,就可以重寫preferredStatusBarStyle的get方法:

override var preferredStatusBarStyle: UIStatusBarStyle{
    get{
        return .lightContent
    }
}
  1. Sign in with Apple -提供第三方登錄的注意啦

如果你的應(yīng)用使用了第三方登錄,那么你可能也需要加下 「Sign in with Apple」
Sign In with Apple will be available for beta testing this summer. It will be required as an option for users in apps that support third-party sign-in when it is commercially available later this year.

  1. 即將廢棄的 LaunchImage

從 iOS 8 的時(shí)候,蘋果就引入了 LaunchScreen,我們可以設(shè)置 LaunchScreen來(lái)作為啟動(dòng)頁(yè)。當(dāng)然,現(xiàn)在你還可以使用LaunchImage來(lái)設(shè)置啟動(dòng)圖。不過(guò)使用LaunchImage的話,要求我們必須提供各種屏幕尺寸的啟動(dòng)圖,來(lái)適配各種設(shè)備,隨著蘋果設(shè)備尺寸越來(lái)越多,這種方式顯然不夠 Flexible。而使用 LaunchScreen的話,情況會(huì)變的很簡(jiǎn)單, LaunchScreen是支持AutoLayout+SizeClass的,所以適配各種屏幕都不在話下。
注意啦??,從2020年4月開始,所有使? iOS13 SDK的 App將必須提供 LaunchScreen,LaunchImage即將退出歷史舞臺(tái)。

  1. MPMoviePlayerController 在iOS 13已經(jīng)不能用了

10.UISegmentedControl 選中的顏色

 if ( @available(iOS 13.0, *)) {
           self.segmentedControl.selectedSegmentTintColor = [UIColor clearColor];

       }else
       {
           self.segmentedControl.tintColor = [UIColor clearColor];

       }
  1. 內(nèi)嵌WebView,一些圖片路徑,文件路徑,不用寫絕對(duì)路徑,直接寫文件名字即可
    http://www.itdecent.cn/p/767e6f2d8435

12.下面這種獲取網(wǎng)絡(luò)狀態(tài)的方法會(huì)發(fā)生崩潰


 UIApplication *app = [UIApplication sharedApplication];
    NSArray *children = nil;
    // 不能用 [[self deviceVersion] isEqualToString:@"iPhone X"] 來(lái)判斷,因?yàn)槟M器不會(huì)返回 iPhone X
    id statusBar = [app valueForKeyPath:@"statusBar"];
    if ([statusBar isKindOfClass:NSClassFromString(@"UIStatusBar_Modern")]) {
        children = [[[statusBar valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews];
    } else {
        children = [[statusBar valueForKey:@"foregroundView"] subviews];
    }
    NSString *state = [[NSString alloc] init];
    int netType = 0;
    for (id child in children) {
        if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
            //獲取到狀態(tài)欄
            netType = [[child valueForKeyPath:@"dataNetworkType"] intValue];
            switch (netType) {
                case 0:
                    state = @"none";
                    break;
                case 1:
                    state = @"2G";
                    break;
                case 2:
                    state = @"3G";
                    break;
                case 3:
                    state = @"4G";
                    break;
                case 5:
                    state = @"WIFI";
                    break;
                default:
                    break;
            }
        }
    }
    
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Xcode11 【W(wǎng)WDC2019 Session】Xcode 11新特性Xcode官方文檔 iOS13的特性 眾...
    _Joeyoung_閱讀 6,156評(píng)論 14 61
  • iOS 13 支持適配的機(jī)型 iPhone X、iPhone XR、iPhone XS、iPhone XS Max...
    不成活不瘋魔閱讀 44,702評(píng)論 22 131
  • iOS 13 如期而至,適配工作可以開展起來(lái)啦。在適配 iOS 13 過(guò)程中,遇到了如下一些問(wèn)題。 1. UITe...
    前行哲閱讀 20,546評(píng)論 48 136
  • 我認(rèn)為 愛(ài)國(guó)首先要發(fā)自內(nèi)心, 其次是表現(xiàn)在語(yǔ)言中, 最后落實(shí)在行動(dòng)上。 我們不能空談愛(ài)國(guó), 也不能激進(jìn)地愛(ài)國(guó), 從...
    樂(lè)陵君閱讀 538評(píng)論 0 2
  • 2018年1月19日,我?guī)е鴨畏磥?lái)到了北京,第一次的北京之旅,總有些迷茫,焦躁,當(dāng)然,幸虧有陪伴我的人兒。 迷迷糊...
    Mr凱先森閱讀 225評(píng)論 0 2

友情鏈接更多精彩內(nèi)容