iOS 15 适配手册

iOS 15上发现,指定的背景色失效了,但滚动控制器的视图时,导航条的背景又出现了。

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).
从 iOS 15 开始,UINavigationBar、UIToolbar 和 UITabBar 将在您的视图控制器的关联滚动视图位于适当的边缘时使用它们的 scrollEdgeAppearance(或者总是如果您的层次结构中没有 UIScrollView,更多内容见下文)。

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.
您必须采用 UIBarAppearance API(自 iOS 13 起可用,针对每种条形类型进行了专门化)来自定义此行为。 UIToolbar 和 UITabBar 为此在 iOS 15 中添加了 scrollEdgeAppearance 属性。

相关修改如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if (@available(iOS 13.0, *)) {
UINavigationBarAppearance *appearance = [[self.navigationBar standardAppearance] copy];;
appearance.backgroundImage = [self imageFromLayer:gradientLayer];
appearance.titleTextAttributes = @{NSFontAttributeName: [GDDConfig sharedInstance].defaultConfig.navTitleFont, NSForegroundColorAttributeName: [GDDConfig sharedInstance].defaultConfig.navTitleColor};
self.navigationBar.standardAppearance = appearance;
/// 根据苹果的注释,如果scrollEdgeAppearance为nil,会默认使用standardAppearance啊。但在 iOS 15 中,必须同时指定standardAppearance和scrollEdgeAppearance才可以。
self.navigationBar.scrollEdgeAppearance = appearance;
self.navigationBar.tintColor = [GDDConfig sharedInstance].defaultConfig.navTitleColor;;
} else {
self.navigationBar.tintColor = [GDDConfig sharedInstance].defaultConfig.navTitleColor;
self.navigationBar.titleTextAttributes = @{NSFontAttributeName: [GDDConfig sharedInstance].defaultConfig.navTitleFont, NSForegroundColorAttributeName: [GDDConfig sharedInstance].defaultConfig.navTitleColor};



[self.navigationBar setBackgroundImage:[self imageFromLayer:gradientLayer] forBarMetrics:UIBarMetricsDefault];
}

UITabBar 背景图失效

UITabBar之前设置的背景图片,老版本可以,iOS 15上表现为空白。

新的API:

1
@property (nonatomic, readwrite, copy, nullable) UITabBarAppearance *scrollEdgeAppearance; //ios 15.0.

相关代码修改如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
UIImage *img = [UIImage imageNamed:@"image"];
if (@available(iOS 13.0, *)) {
UITabBarAppearance *appearance = [[tabBar standardAppearance] copy];
appearance.backgroundImage = img;
appearance.backgroundImageContentMode = UIViewContentModeScaleToFill;
self.tabBar.standardAppearance = appearance;
if (@available(iOS 15.0, *)) {
self.tabBar.scrollEdgeAppearance = appearance;
} else {
// Fallback on earlier versions
}
} else {
// Fallback on earlier versions
[self.tabBar setBackgroundImage:[img imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)]];
}

UITabBarItem 文字颜色失效

新版本中UITabBarItem文字颜色的修改不起作用。

兼容新的 API :

1
@property (nonatomic, readwrite, copy, nullable) UITabBarAppearance *scrollEdgeAppearance; //ios 15.0.

相关代码修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if (@available(iOS 13.0, *)) {
UITabBarAppearance *appearance = [[self.tabBar standardAppearance] copy];
appearance.backgroundColor = [UIColor whiteColor];
UITabBarItemAppearance *itemAppearance = [[UITabBarItemAppearance alloc] init];
itemAppearance.selected.titleTextAttributes = @{NSForegroundColorAttributeName: [GDDConfig sharedInstance].defaultConfig.highlightColor};
appearance.stackedLayoutAppearance = itemAppearance;
self.tabBar.standardAppearance = appearance;
if (@available(iOS 15.0, *)) {
self.tabBar.scrollEdgeAppearance = appearance;
} else {
// Fallback on earlier versions
}

} else {
// Fallback on earlier versions
self.tabBar.tintColor = [GDDConfig sharedInstance].defaultConfig.highlightColor;
}

UITableView

iOS15 UITableView新增了一个新属性:sectionHeaderTopPadding,默认值为automaticDimension,此属性会给每一个 section header 增加一个默认高度,当我们使用UITableViewStylePlain 初始化tableView的时候,系统默认给 section header 增高了22像素。解决方案是调整sectionHeaderTopPadding属性设置,有需要隐藏头部设置为0即可:

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

##

UIButton

UIButton支持更多配置。UIButton.Configuration是一个新的结构体,它指定按钮及其内容的外观和行为。它有许多与按钮外观和内容相关的属性,如cornerStyle、baseForegroundColor、baseBackgroundColor、buttonSize、title、image、subtitle、titlePadding、imagePadding、contentInsets、imagePlacement等。

相关代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = [UIColor greenColor];
if (@available(iOS 15.0, *)) {
UIButtonConfiguration *conf = [UIButtonConfiguration tintedButtonConfiguration];
conf.cornerStyle = UIButtonConfigurationCornerStyleMedium;
[conf setImagePadding:5];
[conf setTitle:@"大标题"];
[conf setSubtitle:@"副标题"];
[conf setImage:[UIImage imageNamed:@"btnImage.png"]];
conf.imagePlacement = NSDirectionalRectEdgeLeading;
button.configuration = conf;
} else {
// Fallback on earlier versions
}

##

UIImage

图片的尺寸变换,相关代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
UIImage *modeImg = [UIImage imageNamed:@"bgImage.png"];
NSLog(@"图片原尺寸%@", NSStringFromCGSize(modeImg.size));

if (@available(iOS 15.0, *)) {
modeImg = [modeImg imageByPreparingThumbnailOfSize:CGSizeMake(220, 100)];
NSLog(@"1.变换后图片原尺寸%@",NSStringFromCGSize(modeImg.size));
[modeImg prepareThumbnailOfSize:CGSizeMake(220, 100) completionHandler:^(UIImage * _Nullable) {
NSLog(@"2.变换后图片原尺寸%@",NSStringFromCGSize(modeImg.size));
}];
} else {
// Fallback on earlier versions
};
//打印值
//2021-12-15 15:48:17.590981+0800 iOS15UI[71637:683127] 图片原尺寸{674, 206}
//2021-12-15 15:48:17.598647+0800 iOS15UI[71637:683127] 1.变换后图片原尺寸{220, 100}
//2021-12-15 15:48:17.600537+0800 iOS15UI[71637:684293] 2.变换后图片原尺寸{220, 100}

##

UISheetPresentationController

增加UISheetPresentationController,通过它可以控制 Modal 出来的 UIViewController 的显示大小,且可以通过拖拽手势在不同大小之间进行切换。

1
2
3
4
5
6
if let presentationController = presentationController as? UISheetPresentationController {
// 显示时支持的尺寸
presentationController.detents = [.medium(), .large()]
// 显示一个指示器表示可以拖拽调整大小
presentationController.prefersGrabberVisible = true
}

CLLocationButton

推出CLLocationButton用于一次性定位授权,该内容内置于CoreLocationUI模块,但如果需要获取定位的详细信息仍然需要借助于CoreLocation。

URLSession

URLSession 推出支持 async/await 的 API,包括获取数据、上传与下载

------ 本文结束------
0%