iOS15适配

作者: CombatReadiness | 来源:发表于2021-11-16 18:02 被阅读0次

    iOS15适配主要是以下几点:
    UINavigationControllerUITabBarControllersectionHeaderTopPaddingUIImageWriteToSavedPhotosAlbum

    其实iOS13之后就有关于nav和tabbar的新API,但是我测试发现有部分API真正生效还是在iOS15之后,iOS15以前的nav和tabbar的部分API就不适用iOS15之后的了,所以我们这儿判别还是以iOS15来区分。

    关于nav和tabbar新的API都涉及到scrollEdgeAppearancestandardAppearance,我们直接看怎么用吧(下面的nav为UINavigationController实例)。

    UINavigationController

    if #available(iOS 15.0, *) {
        let navAppear = UINavigationBarAppearance()
        navAppear.configureWithOpaqueBackground()
        navAppear.backgroundColor = UIColor.red//导航条背景色
        //这儿可以设置shadowColor透明或设置shadowImage为透明图片去掉导航栏的黑线
        navAppear.shadowColor = UIColor.clear
    //    navAppear.shadowImage = UIColor.clear.asImage(CGSize(width: kScreenW, height: 1.0))
        navAppear.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white,NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18.0)]
        nav.navigationBar.scrollEdgeAppearance = navAppear
        nav.navigationBar.standardAppearance = navAppear                
    } else {
        nav.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white,NSAttributedString.Key.font:UIFont.systemFont(ofSize: 18.0)]
        nav.navigationBar.barTintColor = UIColor.red
        nav.navigationBar.shadowImage = UIImage()
    }
    
    extension UIColor {
        func asImage(_ size:CGSize) -> UIImage? {
            var resultImage:UIImage? = nil
            let rect = CGRect(x: 0.0, y: 0.0, width: size.width, height: size.height)
            UIGraphicsBeginImageContextWithOptions(rect.size, false, UIScreen.main.scale)
            guard let context = UIGraphicsGetCurrentContext() else {
                return resultImage
            }
            context.setFillColor(self.cgColor)
            context.fill(rect)
            resultImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return resultImage
        }
    }
    

    UITabBarController

    if #available(iOS 15.0, *) {
        let tabBarAppear = UITabBarAppearance()
        tabBarAppear.configureWithOpaqueBackground()
        tabBarAppear.backgroundColor = UIColor.yellow//标签栏背景色
        tabBarAppear.stackedLayoutAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.green,NSAttributedString.Key.font:UIFont.systemFont(ofSize: 13.0)]
        tabBarAppear.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.red, NSAttributedString.Key.font:UIFont.systemFont(ofSize: 13.0)]
        nav.tabBarItem.scrollEdgeAppearance = tabBarAppear
        nav.tabBarItem.standardAppearance = tabBarAppear
    } else {           
        self.tabBar.barTintColor = UIColor.yellow
    }
    
    • 标签栏未选中与选中图依然是设置nav.tabBarItem.imagenav.tabBarItem.selectedImage,标签栏去掉黑线同导航栏方式.

    sectionHeaderTopPadding

    此属性是iOS15之后提供给UITableView的,默认值22,所以我们要保持原来的单元格起始位置为0,需要重新设置。

    if #available(iOS 15.0, *) {
        self.tableView?.sectionHeaderTopPadding = 0
    }
    

    UIImageWriteToSavedPhotosAlbum

    iOS15之后UIImageWriteToSavedPhotosAlbum存储图片的回调方法- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo,即使保存成功后此处image返回也为空。

    UIImageWriteToSavedPhotosAlbum(UIImage(named: "image")!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)),nil)
    
    @objc func image(_ image:UIImage?,didFinishSavingWithError error:NSError?,contextInfo:AnyObject?) {
        if error != nil {
            print("保存失败")
        } else {
            print("保存成功")
        }
        //保存成功这儿的image返回也为空
        print(error as AnyObject,image as AnyObject,contextInfo as AnyObject)
    }
    

    相关文章

      网友评论

        本文标题:iOS15适配

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