iOS15适配

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//标签栏背景色
    let normalAttrs: [NSAttributedString.Key: Any] = [NSAttributedString.Key.foregroundColor:UIColor.green,NSAttributedString.Key.font:UIFont.systemFont(ofSize: 13.0),NSAttributedString.Key.paragraphStyle:NSParagraphStyle.default]
    let selectedAttrs: [NSAttributedString.Key: Any] = [NSAttributedString.Key.foregroundColor:UIColor.red, NSAttributedString.Key.font:UIFont.systemFont(ofSize: 13.0),NSAttributedString.Key.paragraphStyle:NSParagraphStyle.default]
    tabBarAppear.stackedLayoutAppearance.normal.titleTextAttributes = normalAttrs
    tabBarAppear.stackedLayoutAppearance.selected.titleTextAttributes = selectedAttrs
    //compactInlineLayoutAppearance,当iPhone为横屏时,需要设置此属性
    tabBarAppear.compactInlineLayoutAppearance.normal.titleTextAttributes = normalAttrs
    tabBarAppear.compactInlineLayoutAppearance.selected.titleTextAttributes = selectedAttrs
    //inlineLayoutAppearance,当为iPad时,需要设置此属性,因为iPad标签栏图标文字为左右排列
    tabBarAppear.inlineLayoutAppearance.normal.titleTextAttributes = normalAttrs
    tabBarAppear.inlineLayoutAppearance.selected.titleTextAttributes = selectedAttrs
    nav.tabBarItem.scrollEdgeAppearance = tabBarAppear
    nav.tabBarItem.standardAppearance = tabBarAppear
} else {           
    nav.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor:UIColor.green,NSAttributedString.Key.font:UIFont.systemFont(ofSize: 13.0)], for: .normal)
    nav.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor:UIColor.red,NSAttributedString.Key.font:UIFont.systemFont(ofSize: 13.0)], for: .selected)
    self.tabBar.barTintColor = UIColor.yellow
}
  • 标签栏未选中与选中图依然是设置nav.tabBarItem.imagenav.tabBarItem.selectedImage,标签栏去掉黑线同导航栏方式.

demo可查阅

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 上适配方案,仅做开发记录使用,开发过程中通过使用陆续增加。 iOS15 的适配,很重...
    smile丽语阅读 5,344评论 11 24
  • 本文作为自己准备适配iOS15所用,在开始适配之前,先去学习各位同学的文章,记录在此备用。 1、导航栏UINavi...
    iOS_zy阅读 14,576评论 5 61
  • UINavigationBar 记得一定要 iOS15 和已以前的版本都好好测一下,适配的时候一定要保留之前的导航...
    铁汁红豆阅读 1,081评论 0 2
  • 以iOS15和xcode13为环境基础,iOS15适配的一些更改和调整。 UINavigationBar UITa...
    丿星纟彖彳亍阅读 1,655评论 0 7
  • 1.Xcode13 必须用这个模式 File -> Workspace Setting ->New Build S...
    奶茶大叔阅读 3,960评论 4 26