iOS10-iOS18主要适配回顾

ios18

  • 1、collectioncell崩溃:collectionView返回同一cell之前不允许dequeue多次;
    UICollectionReusableView也一样
expected dequeued view to be returned to the collection view in preparation for display. When the collection view's data source is asked to provide a view for a given index path, ensure that a single view is dequeued and returned to the collection view. Avoid dequeuing views without a request from the collection view. For retrieving an existing view in the collection view, use -[UICollectionView cellForItemAtIndexPath:] or -[UICollectionView supplementaryViewForElementKind:atIndexPath:].

崩溃代码:

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        var  cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
        if indexPath.item == 1{
            cell = collectionView.dequeueReusableCell(withReuseIdentifier: "seccell", for: indexPath)
        }
        return cell
    }

修改后

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        if indexPath.item == 1{
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "seccell", for: indexPath)
            return cell
        }else{
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
            return cell
        }
   
    }
    

ios17 URL的变化

在 iOS 17 之前,URL 初始化时支持的是较久的 RFC 1738/1808 标准,但是 URLComponents 支持的是 RFC 3986 标准,两个标准不同导致 iOS 开发者在某些时候比较困惑,所以苹果在今年的更新中,终于把标准统一了,统一支持 RFC 3986 标准。

变化是什么?
在 iOS 17 之前,URL(string: "Not an URL") 会返回 nil,因为这是一个无效的 URL 格式,但是在 iOS 17 上,这段代码将会返回 Not%an%URL,相当于是把中间的空格进行了一次 encode。中文在之前需要手动url编码,ios17之后也会自动编码。

Apple 在 iOS 17 的 URL 初始化方法中,添加了一个带有 Bool (默认值 true)的新参数 encodingInvalidCharacters,这个参数代表是否 encoding 掉无效的字符,如果传 false,URL 的行为将会和 iOS 16 上一致。
public init?(string: String, encodingInvalidCharacters: Bool)

// iOS 16
let url = URL(string: "Not an URL") // => nil

// iOS 17
let url = URL(string: "Not an URL", encodingInvalidCharacters: false) // => nil

如果你不想改变 URL 的默认行为,从而兼容以前的代码,需要把之前使用 URL 初始化的方法加上 encodingInvalidCharacters: false 参数。

SwiftData

在WWDC23 上,Apple 推出了新框架 SwiftData,这是一个专注于使用声明式代码持久化数据的框架。SwiftData 使用 Swift 的新宏功能,旨在与 SwiftUI 配合使用,代替 CoreData。

@Observable

@Observable本质上是一个宏,它的作用是将一个类转变为可观察的对象。被@Observable标记的类,其内部属性的变化能够被 SwiftUI 框架自动检测到,并且会触发与之相关的视图更新,从而实现数据和视图之间的自动同步。可以说@Observable是专门为 SwiftUI 设计的特性,与 SwiftUI 的视图更新机制紧密集成,在很多场景下能提升数据驱动视图更新的性能和开发体验。

ios16适配

  • APP切换横竖屏问题
    iOS16之前切换横竖屏使用的是UIDevice的setValue:forKey:方法进行切换。但在iOS16后不再支持使用setValue方法设置设备的方向,建议替换为UIWindowScene 的requestGeometryUpdate方法。

///竖屏
if (@available(iOS 16.0, *)) {
    [self.navigationController setNeedsUpdateOfSupportedInterfaceOrientations];
    NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
    UIWindowScene *ws = (UIWindowScene *)array[0];
    UIWindowSceneGeometryPreferencesIOS *geometryPreferences = [[UIWindowSceneGeometryPreferencesIOS alloc] init];
    geometryPreferences.interfaceOrientations = UIInterfaceOrientationMaskPortrait;
    [ws requestGeometryUpdateWithPreferences:geometryPreferences errorHandler:^(NSError * _Nonnull error) {
         //业务代码
    }];
} else {
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationUnknown] forKey:@"orientation"];
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];
}
//横屏
if (@available(iOS 16.0, *)) {
    [self.navigationController setNeedsUpdateOfSupportedInterfaceOrientations];
    NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
    UIWindowScene *ws = (UIWindowScene *)array[0];
    UIWindowSceneGeometryPreferencesIOS *geometryPreferences =     [[UIWindowSceneGeometryPreferencesIOS alloc] init];
    geometryPreferences.interfaceOrientations =     UIInterfaceOrientationMaskLandscapeLeft;
    [ws requestGeometryUpdateWithPreferences:geometryPreferences     errorHandler:^(NSError * _Nonnull error) {
         //业务代码
    }];
} else {
    [[UIDevice currentDevice] setValue:[NSNumber   numberWithInteger:UIDeviceOrientationUnknown] forKey:@"orientation"];
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];
}

ios15适配

  • 1、UITabar、NaBar新增scrollEdgeAppearance,来描述滚动视图滚动到bar边缘时的外观,即使没有滚动视图也需要去指定scrollEdgeAppearance,否则可能导致bar的背景设置无效。具体可以参考UIBarAppearanceUINavigationBar适配
// preparingThumbnail
UIImage(named: "sv.png")?.preparingThumbnail(of: CGSize(width: 200, height: 100))
// prepareThumbnail,闭包中直接获取调整后的UIImage
UIImage(named: "sv.png")?.prepareThumbnail(of: CGSize(width: 200, height: 100)) { image in
    // 需要回到主线程更新UI
}
await UIImage(named: "sv.png")?.byPreparingThumbnail(ofSize: CGSize(width: 100, height: 100))

ios14适配

  • 1、更改了cell布局视图,之前将视图加载在cell上,将会出现contentView遮罩,导致事件无法响应,必须将customView 放在 contentView 上

  • 2、UIDatePicker默认样式不再是以前的,需要设置preferredDatePickerStyle为 UIDatePickerStyleWheels。

  • 3、IDFA必须要用户用户授权处理,否则获取不到IDFA

  • 4、 UIPageControl的变化 具体参考iOS 14 UIPageControl对比、升级与适配

  • 5、在iOS14之后访问相册新增了Limited Photo Library Access 模式,在授权弹窗中增加了 Select Photo 选项。

  • 6、在 iOS 14 之前,UIStackView 的内部 layer 类型是 CATransformLayer,只做布局,不会直接参与渲染。在此之前设置了 UIStackView 的 backgroundColor,也不会有任何视觉效果。而ios14 UIStackView 的内部 layer 已从原来的 CATransformLayer 更改为使用 CALayer ,设置backgroundColor就可以渲染出来了。

ios13适配

-1、 iOS 13 推出暗黑模式,UIKit 提供新的系统颜色和 api 来适配不同颜色模式,xcassets 对素材适配也做了调整

  • 2、支持第三方登录必须,就必须Sign In with Apple

  • 3、MPMoviePlayerController 废弃

  • 4、iOS 13 DeviceToken有变化

  • 5、模态弹出默认不再是全屏。

  • 6、私有方法 KVC 不允许使用

  • 7、蓝牙权限需要申请

  • 8、LaunchImage 被弃用

  • 9、 可以使用UITableViewDiffableDataSource进行局部刷新

  • 9、新出UIBarAppearance统一配置navigation bars、tab bars、 toolbars等bars的外观。之前设置na bar和tab bar外观的方法可能会无效

  • 10、UITableView新增InsetGrouped样式,创建一个分组带间距和圆角的tableView样式

       let tableView = UITableView(frame: .zero, style: .insetGrouped)
        tableView.sectionHeaderHeight = 0
        tableView.sectionFooterHeight = 10
        tableView.layoutMargins = UIEdgeInsets(top: 0, left: 15, bottom: 0, right: 15)

ios12适配

  • 1、C++ 标准库libstdc++相关的3个库(libstdc++、libstdc++.6、libstdc++6.0.9 )废弃,使用libc++代替

  • 2、短信 验证码自动填充api

if (@available(iOS 12.0, *)) {
        codeTextFiled.textContentType = UITextContentTypeOneTimeCode;
    }

ios11适配

  • 1、ViewController的automaticallyAdjustsScrollViewInsets属性被废弃,用scrollView的contentInsetAdjustmentBehavior代替。
  • 2、safeAreaLayoutGuide的引入
  • 3、tableView默认开启了Size-self
  • 4、新增的prefersLargeTitles属性
  • 5、改善圆角,layer新增了maskedCorners属性
  • 6、tableView右滑删除新增api
  • 7、导航条的层级发生了变化。例如在设置titleView时,可能会出现不期望的布局显示。
  • 8、 相册权限变更
    iOS11以前: NSPhotoLibraryUsageDescription:访问相册和存储照片到相册(读写),会出现用户授权; iOS11之后: NSPhotoLibraryUsageDescription:无需添加。默认开启访问相册权限(读),无需用户授权; NSPhotoLibraryAddUsageDescription: 添加内容到相册(写),会出现用户授权;

ios10适配

  • 1、通知统一使用UserNotifications.framework框架

  • 2、UICollectionViewCell的的优化,新增加Pre-Fetching预加载机制

  • 3、苹果加强了对隐私数据的保护,要对隐私数据权限做一个适配,iOS10调用相机,访问通讯录,访问相册等都要在info.plist中加入权限访问描述,不然之前你们的项目涉及到这些权限的地方就会直接crash掉。

  • 4、AVPlayer增加了多个属性,timeControlStatus、
    automaticallyWaitsToMinimizeStalling
    AVPlayer有时候播放不了的问题

  • 5、tabar未选中颜色设置 用 unselectedItemTintColor代替

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。