iOS14適配

2020-09-19 12:04:47

適配1: Cell點選無效

在14上可能出現點選cell上的檢視無法響應的情況.
原因:iOS14更改Cell檢視佈局.將contentView放在最上層,如果將檢視載入在cell上,將會出現contentView遮罩,導致事件無法響應.是在此前關於 contentView 的宣告註釋中,官方已經明確建議開發者將 customView 放在 contentView 上,使 contentView 作為 UITableViewCell 預設的 fatherView。

解決辦法:
1、可以將cell子檢視載入在contentView上(提倡)
2、將contentView設定到最底層 self.sendSubviewToBack(self.contentView)

或者:通過Runtime簡單暴力的方式快速相容了,原理就是在所有Cell addSubView()時,通過runtime攔截改為 contentView.addsubview(),程式碼:

extension UITableViewCell {
    
    class func ios14Bug() {
        
        let sel1 = #selector(UITableViewCell.runtime_addSubview(_:))
        let sel2 = #selector(UITableViewCell.addSubview(_:))
        
        let method1 = class_getInstanceMethod(UITableViewCell.self, sel1)!
        let method2 = class_getInstanceMethod(UITableViewCell.self, sel2)!
        
        let isDid: Bool = class_addMethod(self, sel2, method_getImplementation(method1), method_getTypeEncoding(method1))
        if isDid {
            class_replaceMethod(self, sel1, method_getImplementation(method2), method_getTypeEncoding(method2))
        } else {
            method_exchangeImplementations(method2, method1)
        }
    }
    
    @objc func runtime_addSubview(_ view: UIView) {
        // 判斷不讓 UITableViewCellContentView addSubView自己
        if view.isKind(of: NSClassFromString("UITableViewCellContentView")!) {
            runtime_addSubview(view)
        } else {
            self.contentView.addSubview(view)
        }
    }
}

適配2:UIDatePicker 更新 UI 樣式

iOS 14 中,UIDatePicker UI樣式更新了

適配3:相簿許可權

iOS14 新增了「Limited Photo Library Access」 模式,在授權彈窗中增加了 Select Photo 選項。使用者可以在 App 請求呼叫相簿時選擇部分照片讓 App 讀取。從 App 的視⻆來看,你的相簿裡就只有這幾張照片,App 無法得知其它照片的存在。
重點!!!:許可權提示框會在每次冷啟動後開啟相簿時重新彈出,可以在 info.plist 中設定 PHPhotoLibraryPreventAutomaticLimitedAccessAlert 選項為 YES ,關閉提示

適配4:地理位置

新增了 精確定位 和 模糊定位 的概念,使用者可以手動選擇,模糊定位的誤差約 500m 。可以根據實際功能判斷是否可以接受使用者選擇模糊定位。
如果功能強依賴精確定位,可以在需要的時候呼叫 [CALocationMnanger requestTemporaryFullAccuracyAuthorizationWithPurposeKey:] 單獨請求一次精確定位,使用者可以選擇拒絕授權。所需引數 purposeKey 需要在 info.plist 中設定 NSLocationTemporaryUsageDescriptionDictionary 字典,key 為 purposeKey , value 為對應的話述

許可權部分可以參考

iOS14更新內容及相容裝置