iOS16适配

背景

苹果在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 setValueattemptRotationToDeviceOrientation配合使用来旋转屏幕是没问题的,但是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 横竖屏切换适配

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>