天天看点

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;
}