Xcode 11 & iOS13 適配文檔

iOS13出來有一段時間了,最近才開始適配,著實(shí)有些晚了,記錄下自己踩到的坑; 持續(xù)更新...

[2019-08-26 ]: Xcode11 & iOS13真機(jī)'UIAlertView' Crash

*** Terminating app due to uncaught exception 'NSObjectNotAvailableException', reason: 'UIAlertView is deprecated and unavailable for UIScene based applications, please use UIAlertController!'

處理方式就顯而易見了,替換成UIAlertController,看來標(biāo)記為廢棄的還是盡量不用為好

[2019-08-19]: iOS 13 beta 7 : 子線程Crash,App仍存活

2019-08-15日Apple發(fā)布了iOS 13 beta 7, 收到一系列新的反饋,其中有一個問題比較奇怪,在觸發(fā)某些Case后,相關(guān)功能穩(wěn)定不可用,Debug測試發(fā)現(xiàn)在iOS 13 beta 7中,某個子線程遇到Swift強(qiáng)制解包(nil!)不會導(dǎo)致整個App Crash掉,主線程卡頓一段時間后,仍然可以正常的操作,但是依賴Crash的子線程的任務(wù)會掛掉,具體原因暫時未知,還在調(diào)研。

[2019-07-30]: -w -Xanalyzer -analyzer-disable-all-checks

Xcode11-beta4(僅在4,1、2、3&5沒有這個問題)中可能會遇到如下報錯

<unknown>:0: error: unknown argument: '-w'
<unknown>:0: error: unknown argument: '-Xanalyzer'
<unknown>:0: error: unknown argument: '-analyzer-disable-all-checks'
Command CompileSwiftSources failed with a nonzero exit code
這個是因?yàn)镃ocoapods的inhibit_all_warnings!、:inhibit_warnings => true導(dǎo)致的,需要到Podfiile中注釋掉所有相關(guān)部分,如:

修改前:

# 全局關(guān)閉警告
inhibit_all_warnings! 
pod 'RxSwift', :inhibit_warnings => true

修改后:

# 全局關(guān)閉警告
# inhibit_all_warnings! 
pod 'RxSwift'#, :inhibit_warnings => true

參考鏈接: https://github.com/CocoaPods/CocoaPods/issues/9013


[2019-07-16]: StatusBar

之前可以使用如下方法獲取或修改StatusBar的一些屬性

if let statusBarWindow = UIApplication.shared.value(forKey: "statusBarWindow") as? UIWindow {
    statusBarWindow.alpha = 1 - statusBarWindow.alpha
}

現(xiàn)在會報錯

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'App called -statusBar or -statusBarWindow on UIApplication: this code must be changed as there's no longer a status bar or status bar window. Use the statusBarManager object on the window scene instead.'

找了很久找到一些獲取屬性的方式,但是現(xiàn)在沒法更改屬性了,無論是直接改還是setValue(_: forKey:),都會崩掉。

if #available(iOS 13.0, *) {
    #if swift(>=5.1)
    if let statusBarManager = UIApplication.shared.keyWindow?.windowScene?.statusBarManager,
        let localStatusBar = statusBarManager.perform(Selector(("createLocalStatusBar")))?.takeRetainedValue()
            as? UIView,
        let statusBar = localStatusBar.perform(Selector(("statusBar")))?.takeRetainedValue() as? UIView,
        let _statusBar = statusBar.value(forKey: "_statusBar") as? UIView {
        print(localStatusBar, statusBar, _statusBar)
    }
    #endif
} else {
    // Fallback on earlier versions
    if let statusBarWindow = UIApplication.shared.value(forKey: "statusBarWindow") as? UIWindow {
        statusBarWindow.alpha = 1 - statusBarWindow.alpha
    }
}

私有屬性KVC

使用私有API一時爽,一直使用一直爽 蘋果一禁火葬場

iOS不再支持使用valueForKeysetValue: forKey等方式處理一些私有屬性,下面列舉一下已經(jīng)發(fā)現(xiàn)的和替換方案。

UITextFiled: _placeholderLabel.textColor

// old code Swift
textFiled.setValue(UIColor.red, forKey: "_placeholderLabel.textColor")

替換方案

textFiled.attributedPlaceholder = NSMutableAttributedString(
    string: "Placeholder",
    attributes: [.foregroundColor: UIColor.red]
)

UISearchBar: value(forKey: "_searchField")

// old  code Swift
searchBar.value(forKey: "_searchField")

替換方案

#if swift(<5.1)
print(searchBar.value(forKey: "_searchField") ?? "Read Error")
#else
print(searchBar.searchTextField)
#endif

樣式 Or 行為變更

UISegmentedControl 樣式變更下圖

UISegmentedControl 樣式對比

處理方案:

    // swift 使用下面的方法自定義樣式
    control.setTitleTextAttributes(
        [.foregroundColor: UIColor.white],
        for: .selected)

    control.setBackgroundImage(
        UIImage(named: "iamgeName"),
        for: .normal,
        barMetrics: .default)

    control.setDividerImage(
        UIImage(named: "iamgeName"),
        forLeftSegmentState: .normal,
        rightSegmentState: .normal,
        barMetrics: .default)

UIModalPresentationStyle

ViewControllerpresent 的默認(rèn)style變了,如下圖:

UIModalPresentationStyle 默認(rèn)樣式

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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