美文网首页iOS开发技术文章
iOS 15 适配和新特性

iOS 15 适配和新特性

作者: 阳光下的灰尘 | 来源:发表于2021-07-26 11:35 被阅读0次

    1、苹果对导航栏的性能做了优化,默认情况下,如果导航栏与视图没有折叠,导航栏的背景透明,如果系统检测到有重叠的话,会变成毛玻璃的效果

    if (@available(iOS 15.0, *)) {
    NSDictionary *textAttr = @{
                                       NSForegroundColorAttributeName : [UIColor whiteColor],
                                       NSFontAttributeName: [UIFont systemFontOfSize:17],
                                       };
        UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
        [appearance setShadowImage:[[UIImage alloc] init]];
    appearance.titleTextAttributes = textAttr;
        [appearance setBackgroundColor:TAD_THM.navigationBackgroundColor];
        // 隐藏分割线 设置一个透明或者纯色的图片 设置nil 或者 [UIImage new]无效
        [appearance setBackgroundImage:[UIImage zt_imageWithPureColor:[UIColor whiteColor]]];
        [appearance setShadowImage:[UIImage zt_imageWithPureColor:[UIColor whiteColor]]];
    
        [[UINavigationBar appearance] setScrollEdgeAppearance: appearance];
    // 滚动页面 防止导航栏会变白色
    [[UINavigationBar appearance] setStandardAppearance:appearance];
    
            [[UINavigationBar appearance] setCompactAppearance:appearance];
    
    // 隐藏导航栏底部一个线条
                [self.navigationController.navigationBar.subviews.firstObject.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    if ([obj isKindOfClass:NSClassFromString(@"_UIBarBackgroundShadowContentImageView")] || [obj isKindOfClass:NSClassFromString(@"_UIBarBackgroundShadowView")]) {
                        obj.hidden=YES;
                    }
                }];
    }
    

    颜色转图片

    + (UIImage *)zt_imageWithPureColor:(UIColor *)color {
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(3, 3), NO, [UIScreen mainScreen].scale);
        UIBezierPath* p = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 3, 3)];
        [color setFill];
        [p fill];
        UIImage* img = UIGraphicsGetImageFromCurrentImageContext();
        return img;
    }
    + (UIImage *)zt_imageWithPureColor:(UIColor *)color size:(CGSize )size{
        UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
        UIBezierPath* p = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, size.width, size.height)];
        [color setFill];
        [p fill];
        UIImage* img = UIGraphicsGetImageFromCurrentImageContext();
        return img;
    }
    

    UINavigationBar默认是透明的,有滑动时会逐渐变为模糊效果,可以通过改变scrollEdgeAppearance属性直接变为模糊效果

    if (@available(iOS 15.0, *)){
        UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
        appearance.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleRegular];
        navBar.scrollEdgeAppearance = appearance;
    }
    

    2、UITableView新增了一条新属性:sectionHeaderTopPadding, 默认会给每一个section header 增加一个高度,当我们使用 UITableViewStylePlain 初始化UITableView的时候,能发现sectionHeader增高了22px。解决办法就是手动去除这个高度

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

    全局适配设置

    if (@available(iOS 15.0, *)) {
        //设置默认的分组头部间隙为0
        [UITableView appearance].sectionHeaderTopPadding = CGFLOAT_MIN;
    }
    

    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...
    }
    

    相关文章

      网友评论

        本文标题:iOS 15 适配和新特性

        本文链接:https://www.haomeiwen.com/subject/mwnwmltx.html