iOS15適配記錄篇

本文作為自己準備適配iOS15所用,在開始適配之前,先去學習各位同學的文章,記錄在此備用。

1、導航欄UINavigationBar

從 iOS 15 開始,UINavigationBar、UIToolbar 和 UITabBar 在控制器中關聯滾動視圖頂部或底部時使用
在iOS15中,UINavigationBar默認是透明的,有滑動時會逐漸變為模糊效果,可以通過改變UINavigationBar.scrollEdgeAppearance屬性直接變為模糊效果、配置相關屬性-背景、字體等

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

用新xcode13編譯工程后,導航欄的問題比較明顯,調試之后發現是UINavigationBar部分屬性的設置在iOS15上是無效的

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

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

  • 新代碼
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  // 設置導航欄背景色
    app.shadowImage = UIColor.clear.image  // 設置導航欄下邊界分割線透明
    navigationBar.scrollEdgeAppearance = app  // 帶scroll滑動的頁面
    navigationBar.standardAppearance = app // 常規頁面。描述導航欄以標準高度顯示時要使用的外觀屬性。
}

關于navigationBar背景圖片失效的問題:
老代碼:

navigationBar.setBackgroundImage(img, for: .default)

iOS15代碼:

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

tabbar跟navigationBar同理設置即可。

2、TableView

從 iOS 15 開始,TableView 增加sectionHeaderTopPadding屬性,默認情況sectionHeaderTopPadding會有22個像素的高度,及默認情況,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存儲圖片之后的回調不再返回圖片了,會返回nil,如果在回調方法里面操作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背景顏色設置失效,字體設置失效,陰影設置失效問題

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

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

  • 新代碼
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 // 設置選中attributes
    self.tabBar.scrollEdgeAppearance = bar
    self.tabBar.standardAppearance = bar//與navi同理
}

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

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

9、UIButton支持更多配置。UIButton.Configuration是一個新的結構體,它指定按鈕及其內容的外觀和行為。它有許多與按鈕外觀和內容相關的屬性,如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用于一次性定位授權,該內容內置于CoreLocationUI模塊,但如果需要獲取定位的詳細信息仍然需要借助于CoreLocation。

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

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

// 加載數據
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、系統圖片支持多個層,支持多種渲染模式。

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

13、UIImage 新增了幾個調整尺寸的方法。

// preparingThumbnail
UIImage(named: "sv.png")?.preparingThumbnail(of: CGSize(width: 200, height: 100))
// prepareThumbnail,閉包中直接獲取調整后的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))

比較實用的9,13,之前都是封裝了好多代碼,現在一句話搞定

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,030評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,310評論 3 415
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,951評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,796評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,566評論 6 407
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,055評論 1 322
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,142評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,303評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,799評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,683評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,899評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,409評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,135評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,520評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,757評論 1 282
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,528評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,844評論 2 372

推薦閱讀更多精彩內容