iOS 15-適配要點

  1. 增長UISheetPresentationController,經過它能夠控制 Modal 出來的 UIViewController 的顯示大小,且能夠經過拖拽手勢在不一樣大小之間進行切換。只須要在跳轉的目標 UIViewController 作以下處理:
if let presentationController = presentationController as? UISheetPresentationController {
       // 顯示時支持的尺寸
       presentationController.detents = [.medium(), .large()]
       // 顯示一個指示器表示能夠拖拽調整大小
       presentationController.prefersGrabberVisible = true
}
複製代碼
  1. 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) 
複製代碼

Snipaste_2021-07-11_15-26-55.png 16259886795922.png

  1. 推出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)
複製代碼
  1. URLSession 推出支持 async/await 的 API,包括獲取數據、上傳與下載。
let session = URLSession.shared
// 加載數據
let (data, response) = try await session.data(from: url)
// 下載
let (localURL, _) = try await session.download(from: url)
// 上傳
let (_, response) = try await session.upload(for: request, from: data)
複製代碼
  1. 系統圖片支持多個層,支持多種渲染模式。
// 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)
複製代碼
  1. UINavigationBar、UIToolbar 和 UITabBar 設置顏色,須要使用 UIBarAppearance APIs。
// UINavigationBar
let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.backgroundColor = .red
navigationController?.navigationBar.scrollEdgeAppearance = navigationBarAppearance
navigationController?.navigationBar.standardAppearance = navigationBarAppearance
// UIToolbar
let toolBarAppearance = UIToolbarAppearance()
toolBarAppearance.backgroundColor = .blue
navigationController?.toolbar.scrollEdgeAppearance = toolBarAppearance
navigationController?.toolbar.standardAppearance = toolBarAppearance
// UITabBar
let tabBarAppearance = UITabBarAppearance()
toolBarAppearance.backgroundColor = .purple
tabBarController?.tabBar.scrollEdgeAppearance = tabBarAppearance
tabBarController?.tabBar.standardAppearance = tabBarAppearance
複製代碼
  1. UITableView 新增了屬性 sectionHeaderTopPadding,會給每個section 的 header 增長一個默認高度。
tableView.sectionHeaderTopPadding = 0
複製代碼
  1. 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))
複製代碼

文中代碼已在 Xcode 13 Beta3 中測試經過, 案例源代碼下載地址git

相關文章
相關標籤/搜索