iOS15适配主要是以下几点:
UINavigationController
、UITabBarController
、sectionHeaderTopPadding
、UIImageWriteToSavedPhotosAlbum
其实iOS13之后就有关于nav和tabbar的新API,但是我测试发现有部分API真正生效还是在iOS15之后,iOS15以前的nav和tabbar的部分API就不适用iOS15之后的了,所以我们这儿判别还是以iOS15来区分。
关于nav和tabbar新的API都涉及到scrollEdgeAppearance
和standardAppearance
,我们直接看怎么用吧(下面的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.image
和nav.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)
}