irpas技术客

iOS16适配_心若向阳_无谓悲伤_ios 16 适配

未知 8118

背景

苹果在9月13号凌晨(北京时间)发布 iOS 16,该系统的设备可能会因为各种原因,导致功能不可用和UI错乱等问题,我们需要做好适配iOS 16。

前置条件

开发者对 iOS 16 模拟器和真机调试的前置条件:

需要安装?Xcode 14 或者下载真机调试包

iOS 16 真机调试时需要在设备的设置 —> 隐私与安全 —> 开发者模式?中打开开发者模式。

UIDevice (屏幕旋转)

[New Features] iOS apps can now request rotation using?[UIWindowScene requestGeometryUpdate:errorHandler:]?and providing a?UIWindowSceneGeometryPreferencesIOS?object containing the desired orientations. (95229615)

[Deprecations][UIViewController shouldAutorotate]?has been deprecated is no longer supported.?[UIViewController attemptRotationToDeviceOrientation]?has been deprecated and replaced with?[UIViewController setNeedsUpdateOfSupportedInterfaceOrientations].

iOS16 横竖屏切换适配

经测试,目前使用UIDevice setValue和attemptRotationToDeviceOrientation配合使用来旋转屏幕是没问题的,但是attemptRotationToDeviceOrientation 是即将废弃的(未来某个版本可能会不可用),推荐用shouldAutorotateToInterfaceOrientation替代shouldAutorotate,用setNeedsUpdateOfSupportedInterfaceOrientations替换attemptRotationToDeviceOrientation。

setNeedsUpdateOfSupportedInterfaceOrientations和[UIWindowScene requestGeometryUpdate:errorHandler:]需要在Xcode 14和#available(iOS 16.0, *)环境才能编译

使用?UIDevice.current.setValue(value, forKey: "orientation"),真机环境调试 iOS 16,旋转屏幕控制台会收到警告:

2022-09-15 17:52:57.588159+0800 italki[7177:1083152] [Orientation] BUG IN CLIENT OF UIKIT: Setting UIDevice.orientation is not supported. Please use UIWindowScene.requestGeometryUpdate(_:)

UIScreen

@property(class, nonatomic, readonly) UIScreen *mainScreen API_DEPRECATED("Use a UIScreen instance found through context instead: i.e, view.window.windowScene.screen", ios(2.0, API_TO_BE_DEPRECATED)); // the device's internal screen

UIScreen.main即将被废弃,建议使用(UIApplication.shared.connectedScenes.first as? UIWindowScene)?.screen

UISearchBarController

从iOS16起,UISearchBar在iPad上发生了变化,在导航栏的右边,Cancel按钮没有居中。

其他新特性

新增 UICalendarView,可以显示日期并支持单选与多选日期。

新增 UIPasteControl 用于读取剪贴板中的内容,否则跨 App 读取时会弹出对话框让用户进行选择是否同意。

UINavigationItem 变化iOS16适配指南之UINavigationItem - 掘金

增加了一个属性titleMenuProvider用于给当前导航栏的标题添加操作菜单。

// backAction用于实现当前 UIViewController 的返回按钮事件 navigationItem.backAction = UIAction(handler: { _ in self.navigationController?.popViewController(animated: true) })

增加了一个属性backAction用于实现当前 UIViewController 的返回按钮事件;

// 用于给当前导航栏的标题添加操作菜单 navigationItem.titleMenuProvider = { _ in let favorite = UIAction(title: "Favorite", image: UIImage(systemName: "heart.fill")) { _ in print("favorite") } let share = UIAction(title: "Share", image: UIImage(systemName: "square.and.arrow.up.fill")) { _ in print("share") } let delete = UIAction(title: "Delete", image: UIImage(systemName: "trash.fill"), attributes: [.destructive]) { _ in print("delete") } return UIMenu(children: [favorite, share, delete]) }

增加了一个属性style用于描述 UINavigationItem 在 UINavigationBar 上的布局;

// 设置style navigationItem.style = .editor // browser, navigator

隐私权限增强,如通过 UIDevice 获取设备名称时,无法获取用户的信息,只能获取设备对应的名称。

UIDevice 不再支持通过setValue()方法设置设备的方向,替换为 UIWindowScene 的requestGeometryUpdate()方法。

新增一个交互 UIEditMenuInteraction,用于取代 UIMenuController 与 UIMenuItem。iOS16适配指南之UIEditMenuInteraction - 掘金

新增一个交互 UIFindInteraction 用于文本内容查找与替换,UITextView、WKWebView 与 PDFView 已经默认支持,但需要将其isFindInteractionEnabled属性设置为true。iOS16适配指南之UIFindInteraction - 掘金

新增 LARightStore 用于存储与获取 keychain 中的数据。iOS16适配指南之LARightStore - 简书

UIImage 增加了新的构造函数用于支持 SF Symbols 最新版中增加的类别 Variable。(如Wifi信号图标)

UIPageControl 支持垂直显示并可以设置指示器与当前页的图片。

UITableView 与 UICollectionView 增加了新的selfSizingInvalidation参数,通过它 Cell 具备自动调整大小的能力。

UITableView 与 UICollectionView 在使用 Cell Content Configuration 时支持使用 UIHostingConfiguration 包装 SwiftUI 代码定义 Cell 的内容。

cell.contentConfiguration = UIHostingConfiguration { ??? HStack { ??????? Image(systemName: images[indexPath.row]) ??????????? .foregroundStyle(.teal) ??????? Text(devices[indexPath.row]) ??????????? .font(.caption) ??????????? .foregroundStyle(.secondary) ??? } }

UISheetPresentationController 支持自定义显示的 UIViewController 的大小。

UIMenu 支持设置尺寸,分别为small、medium与large。

let addNewMenu = UIMenu(title: "", preferredElementSize: .small, children: menuActions)

支持 Live Activity,可以理解为一种特殊的锁屏界面显示的 Widget。

?参考文献

What's new in UIKit - WWDC22 - Videos - Apple Developer

Apple Developer Documentation

iOS16 适配指南 - 掘金

iOS16适配指南之UINavigationItem - 掘金

iOS16适配指南之LARightStore - 简书

iOS16适配指南之UIFindInteraction - 掘金

iOS16适配指南之UIEditMenuInteraction - 掘金

Apple Developer Documentation

??????iOS16 横竖屏切换适配


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #iOS #16 #适配