天天看点

常用知识之iOS15代码、功能、属性新适配

环境工具:iOS15、Xcode13

1、UINavigationBar、UITabBar、UIToolBar

部分属性在iOS15上是无效的,比如:导航栏颜色、字体颜色、阴影设置等,需要使用iOS13更新的API中UINavigationBarAppearance、UITabBarAppearance的实例对象来设置。

对导航栏的性能做了优化,默认情况下,如果导航栏与视图没有重叠,导航栏的背景透明,如果重叠,会变成模糊的效果。如果想要一直就是模糊效果,可以通过改变scrollEdgeAppearance属性来实现。UINavigationBar、UITabBar、UIToolBar将在UIViewController关联滚动视图位于适当的边缘时使用scrollEdgeAppearance属性。

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、UITableView新增一个属性:sectionHeaderTopPadding,作为列表每个部分标题上方的填充,默认会给每个sectionheader增加一个高度:22px,当使用UITableViewStylePlain初始化UITableView时,会发现这个现象。因为默认属性是UITableViewAutomaticDimension。可以手动去除此高度。

if (@available(iOS 15.0, *)) {
    table.sectionHeaderTopPadding = 0;
}
           

3、UIImageWriteToSavedPhotosAlbum存储图片之后的回调不再返回图片了,会返回nil,如果在回调方法里面操作image有可能会直接Crash,目前的解决办法声明一个全局image去记录,再去操作。

self.image = image;
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
            
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    // self.image doing...
}
           

4、UIButton.configuration是一个新的结构体,它可以配置按钮的属性和行为,比如:title、image、buttonSize、cornerStyle、imagePadding、titlePadding等。

5、Json解析支持Json5。

6、UICollectionViewCell、UITableViewCell支持UICellConfigurationState状态变化时的Block执行。

7、UICollectionViewLayout支持自动高度:automaticDimension。

8、系统图片支持多个层,支持多种渲染模式。

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

10、UILabel显示的文字比设置Font大,在iOS 15中,UILabel 设置adjustsFontSizeToFitWidth为true时,高度不能跟设置的 Font 一样大。固定宽度的UILabel文字显示不全,在iOS 15以下计算的正好显示完UILabel文字的宽度,在iOS 15上可能显示不全,需要增加宽度。

继续阅读