天天看點

iOS 15 UI适配

前言

iOS 15在2021 WWDC會後釋出,就勇猛的把水果全家桶都更新了最新系統。兩個iOS 15 beta版本過後,系統穩定性整體還不錯。也随之發現了幾個iOS适配上的bug,在此整理記錄下來。後續有發現再繼續補充。

Xcode Version 13.0 beta

iOS 15 Developer Beta2

1. UINavigationBar

  • 在iOS 15中,UINavigationBar預設為透明。在滑動時會有模糊效果。如果想要一直就是模糊效果,可以通過改變scrollEdgeAppearance屬性來實作。

解決辦法:

UINavigationBarAppearance *barApp = [[UINavigationBarAppearance alloc] init];
barApp.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleRegular];
self.navigationBar.scrollEdgeAppearance = barApp;
           
  • NavigationBar顔色設定無效
self.navigationController.navigationBar.barTintColor = [[UIColor blueColor] colorWithAlphaComponent:0.5];
           

往常我們用以上代碼來設定導航欄和狀态欄背景色,此代碼在iOS 15的無效。

apple developer forums

As of iOS 15, UINavigationBar, UIToolbar, and UITabBar will use their scrollEdgeAppearance when your view controller’s associated scroll view is at the appropriate edge (or always if you don’t have a UIScrollView in your hierarchy, more on that below).
You must adopt the UIBarAppearance APIs (available since iOS 13, specializations for each bar type) to customize this behavior. UIToolbar and UITabBar add scrollEdgeAppearance properties for this purpose in iOS 15.

從 iOS 15 開始,UINavigationBar、UIToolbar 和 UITabBar 将在你的VC關聯滾動視圖位于适當的邊緣時使用 scrollEdgeAppearance(或者如果您的試圖層級結構中沒有 UIScrollView,更多内容見下文)。

您必須使用 UIBarAppearance API 來自定義。UIToolbar 和 UITabBar 為此在 iOS 15 中添加了 scrollEdgeAppearance 屬性。

/// Describes the appearance attributes for the tabBar to use when an observable scroll view is scrolled to the bottom. If not set, standardAppearance will be used instead.

@property (nonatomic, readwrite, copy, nullable) UITabBarAppearance *scrollEdgeAppearance UI_APPEARANCE_SELECTOR API_AVAILABLE(ios(15.0));

/// Describes the appearance attributes for the toolbar to use at standard height when an observable scroll view is scrolled to the bottom. If not set, standardAppearance will be used instead.

@property (nonatomic, readwrite, copy, nullable) UIToolbarAppearance *scrollEdgeAppearance UI_APPEARANCE_SELECTOR API_AVAILABLE(ios(15.0));

解決辦法:

if (@available(iOS 15.0, *)) {
        UINavigationBarAppearance *barApp = [UINavigationBarAppearance new];
        barApp.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.5];
        self.navigationController.navigationBar.scrollEdgeAppearance = barApp;
        self.navigationController.navigationBar.standardAppearance = barApp;
    }
           

2. iOS 15 UITableView sectionHeader下移22像素

iOS 15中 UITableView 新增了一個屬性:sectionHeaderTopPadding。此屬性會給每一個 section header 增加一個預設高度,當我們使用 UITableViewStylePlain 初始化UITableView 的時候,系統預設給 section header 增高了22像素。

/// Padding above each section header. The default value is

UITableViewAutomaticDimension

.

@property (nonatomic) CGFloat sectionHeaderTopPadding API_AVAILABLE(ios(15.0), tvos(15.0), watchos(8.0));

解決辦法:

if (@available(iOS 15.0, *)) {
    tableView.sectionHeaderTopPadding = 0;
}