iOS15適配記錄篇

本文作為自己準(zhǔn)備適配iOS15所用,在開始適配之前,先去學(xué)習(xí)各位同學(xué)的文章,記錄在此備用。

1、導(dǎo)航欄UINavigationBar

從 iOS 15 開始,UINavigationBar、UIToolbar 和 UITabBar 在控制器中關(guān)聯(lián)滾動視圖頂部或底部時使用
在iOS15中,UINavigationBar默認(rèn)是透明的,有滑動時會逐漸變?yōu)槟:Ч梢酝ㄟ^改變UINavigationBar.scrollEdgeAppearance屬性直接變?yōu)槟:Ч?、配置相關(guān)屬性-背景、字體等

if #available(iOS 15.0, *) { //UINavigationBarAppearance屬性從iOS13開始
      let navBarAppearance = UINavigationBarAppearance()
      // 背景色
      navBarAppearance.backgroundColor = UIColor.clear
      // 去掉半透明效果
      navBarAppearance.backgroundEffect = nil
      // 去除導(dǎo)航欄陰影(如果不設(shè)置clear,導(dǎo)航欄底下會有一條陰影線)
      navBarAppearance.shadowColor = UIColor.clear
      // 字體顏色
      navBarAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
      self.navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
}

用新xcode13編譯工程后,導(dǎo)航欄的問題比較明顯,調(diào)試之后發(fā)現(xiàn)是UINavigationBar部分屬性的設(shè)置在iOS15上是無效的

  • 舊代碼
navigationBar.setBackgroundImage(UIColor.clear.image, for: .default)
// 導(dǎo)航欄背景,主題色是綠色
navigationBar.barTintColor = UIColor.theme
// 默認(rèn)不透明
navigationBar.isTranslucent = false
// 著色,讓返回按鈕圖片渲染為白色
navigationBar.tintColor = UIColor.white
// 導(dǎo)航欄文字
navigationBar.titleTextAttributes = [
     NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18),
     NSAttributedString.Key.foregroundColor: UIColor.white
]

run起來后發(fā)現(xiàn),導(dǎo)航欄顏色設(shè)置沒有作用,呈現(xiàn)是白色,字體顏色也沒有生效,呈現(xiàn)黑色,查看導(dǎo)航欄特性API:UINavigationBarAppearance后發(fā)現(xiàn),iOS15navigationBar的相關(guān)屬性設(shè)置要通過實(shí)例UINavigationBarAppearance來實(shí)現(xiàn),UINavigationBarAppearance是iOS13更新的API,應(yīng)該有人已經(jīng)在用,我們的應(yīng)用兼容iOS10以上,對于導(dǎo)航欄的設(shè)置還沒有使用UINavigationBarAppearance,如今在iOS15上失效,所以對于呈現(xiàn)的問題,做如下適配:

  • 新代碼
if #available(iOS 15, *) {
    let app = UINavigationBarAppearance.init()
    app.configureWithOpaqueBackground()  // 重置背景和陰影顏色
    app.titleTextAttributes = [
        NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18),
        NSAttributedString.Key.foregroundColor: UIColor.white
    ]
    app.backgroundColor = UIColor.theme  // 設(shè)置導(dǎo)航欄背景色
    app.shadowImage = UIColor.clear.image  // 設(shè)置導(dǎo)航欄下邊界分割線透明
    navigationBar.scrollEdgeAppearance = app  // 帶scroll滑動的頁面
    navigationBar.standardAppearance = app // 常規(guī)頁面。描述導(dǎo)航欄以標(biāo)準(zhǔn)高度顯示時要使用的外觀屬性。
}

關(guān)于navigationBar背景圖片失效的問題:
老代碼:

navigationBar.setBackgroundImage(img, for: .default)

iOS15代碼:

appearance.backgroundImage = img;
appearance.backgroundImageContentMode = UIViewContentModeScaleToFill;
navigationBar.standardAppearance = appearance;

tabbar跟navigationBar同理設(shè)置即可。

2、TableView

從 iOS 15 開始,TableView 增加sectionHeaderTopPadding屬性,默認(rèn)情況sectionHeaderTopPadding會有22個像素的高度,及默認(rèn)情況,TableView section header增加22像素的高度

/// Padding above each section header. The default value is `UITableViewAutomaticDimension`.
    @available(iOS 15.0, *)
    open var sectionHeaderTopPadding: CGFloat
if #available(iOS 15.0, *) {
      self.tableView.sectionHeaderTopPadding = 0
}

3、Image

在iOS15中,UIImageWriteToSavedPhotosAlbum存儲圖片之后的回調(diào)不再返回圖片了,會返回nil,如果在回調(diào)方法里面操作image有可能會直接Crash,目前的解決辦法聲明一個全局image去記錄,后面再去操作

self.image = image;
UIImageWriteToSavedPhotosAlbum(image,self,@selector(image:didFinishSavingWithError:contextInfo:), NULL);
            
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    // self.image doing...
}

4、UITabbar

tabbar的問題和navigationBar的問題屬于同一類,tabbar背景顏色設(shè)置失效,字體設(shè)置失效,陰影設(shè)置失效問題

  • 舊代碼
self.tabBar.backgroundImage = UIColor.white.image
self.tabBar.shadowImage = UIColor.init(0xEEEEEE).image
item.setTitleTextAttributes(norTitleAttr, for: .normal)
item.setTitleTextAttributes(selTitleAttr, for: .selected)

首先是背景色設(shè)置失效,讓我就想到了navigationbar的問題,所以沒有查api了
直接用UITabBarAppearance來設(shè)置,

  • 新代碼
if #available(iOS 15, *) {
    let bar = UITabBarAppearance.init()
    bar.backgroundColor = UIColor.white
    bar.shadowImage = UIColor.init(0xEEEEEE).image
    let selTitleAttr = [
        NSAttributedString.Key.font: itemFont,
        NSAttributedString.Key.foregroundColor: UIColor.theme
    ]
    bar.stackedLayoutAppearance.selected.titleTextAttributes = selTitleAttr // 設(shè)置選中attributes
    self.tabBar.scrollEdgeAppearance = bar
    self.tabBar.standardAppearance = bar//與navi同理
}

5、對狀態(tài)編程的支持:UICellConfigurationState;UICollectionViewCell、UITableViewCell都支持狀態(tài)變化時的block執(zhí)行了。
6、UICollectionViewLayout支持自動高度;AutomaticDimension
7、json解析支持json5了
8、增加UISheetPresentationController,通過它可以控制 Modal 出來的 UIViewController 的顯示大小,且可以通過拖拽手勢在不同大小之間進(jìn)行切換。只需要在跳轉(zhuǎn)的目標(biāo) UIViewController 做如下處理:

if let presentationController = presentationController as? UISheetPresentationController {
   // 顯示時支持的尺寸
   presentationController.detents = [.medium(), .large()]
   // 顯示一個指示器表示可以拖拽調(diào)整大小
   presentationController.prefersGrabberVisible = true
}

9、UIButton支持更多配置。UIButton.Configuration是一個新的結(jié)構(gòu)體,它指定按鈕及其內(nèi)容的外觀和行為。它有許多與按鈕外觀和內(nèi)容相關(guān)的屬性,如cornerStyle、baseForegroundColor、baseBackgroundColor、buttonSize、title、image、subtitle、titlePadding、imagePadding、contentInsets、imagePlacement等。

// Plain
let plain = UIButton(configuration: .plain(), primaryAction: nil)
plain.setTitle("Plain", for: .normal)
// Gray
let gray = UIButton(configuration: .gray(), primaryAction: nil)
gray.setTitle("Gray", for: .normal)
// Tinted
let tinted = UIButton(configuration: .tinted(), primaryAction: nil)
tinted.setTitle("Tinted", for: .normal)
// Filled
let filled = UIButton(configuration: .filled(), primaryAction: nil)
filled.setTitle("Filled", for: .normal) 
image

10、推出CLLocationButton用于一次性定位授權(quán),該內(nèi)容內(nèi)置于CoreLocationUI模塊,但如果需要獲取定位的詳細(xì)信息仍然需要借助于CoreLocation。

let locationButton = CLLocationButton()
// 文字
locationButton.label = .currentLocation
locationButton.fontSize = 20
// 圖標(biāo)
locationButton.icon = .arrowFilled
// 圓角
locationButton.cornerRadius = 10
// tint
locationButton.tintColor = UIColor.systemPink
// 背景色
locationButton.backgroundColor = UIColor.systemGreen
// 點(diǎn)擊事件,應(yīng)該在在其中發(fā)起定位請求
locationButton.addTarget(self, action: #selector(getCurrentLocation), for: .touchUpInside)

11、URLSession 推出支持 async/await 的 API,包括獲取數(shù)據(jù)、上傳與下載。

// 加載數(shù)據(jù)
let (data, response) = try await URLSession.shared.data(from: url)
// 下載
let (localURL, _) = try await session.download(from: url)
// 上傳
let (_, response) = try await session.upload(for: request, from: data)

12、系統(tǒng)圖片支持多個層,支持多種渲染模式。

// hierarchicalColor:多層渲染,透明度不同
let config = UIImage.SymbolConfiguration(hierarchicalColor: .systemRed)
let image = UIImage(systemName: "square.stack.3d.down.right.fill", withConfiguration: config)
// paletteColors:多層渲染,設(shè)置不同風(fēng)格
let config2 = UIImage.SymbolConfiguration(paletteColors: [.systemRed, .systemGreen, .systemBlue])
let image2 = UIImage(systemName: "person.3.sequence.fill", withConfiguration: config2)

13、UIImage 新增了幾個調(diào)整尺寸的方法。

// preparingThumbnail
UIImage(named: "sv.png")?.preparingThumbnail(of: CGSize(width: 200, height: 100))
// prepareThumbnail,閉包中直接獲取調(diào)整后的UIImage
UIImage(named: "sv.png")?.prepareThumbnail(of: CGSize(width: 200, height: 100)) { image in
    // 需要回到主線程更新UI
}
// byPreparingThumbnail
await UIImage(named: "sv.png")?.byPreparingThumbnail(ofSize: CGSize(width: 100, height: 100))

比較實(shí)用的9,13,之前都是封裝了好多代碼,現(xià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ù)。

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

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