1. UINavigationBar
在iOS 15中,UINavigationBar默认为透明,在滑动时会有模糊效果。如果想一直是模糊效果可以通过设置scrollEdgeAppearance属性来实现。
let barApp = UINavigationBarAppearance.init()
barApp.backgroundEffect = UIBlurEffect.init(style: .regular)
self.navigationBar.scrollEdgeAppearance = barApp*
并且部分旧的navigationBar属性设置无效
-
旧代码
let navigationBar: UINavigationBar = UINavigationBar.init() navigationBar.backgroundColor = .red navigationBar.isTranslucent = false navigationBar.tintColor = .white navigationBar.titleTextAttributes = [ NSAttributedString.Key.font : UIFont.systemFont(ofSize: 18.0), NSAttributedString.Key.foregroundColor : UIColor.white ]
编译发现,导航栏颜色设置没用作用,呈现白色。导航栏字体颜色设置也没有生效,呈现黑色。查看API发现,iOS 15 UINavigationBar 的相关属性设置需要通过UINavigationBarAppearance来实现。所以需要对iOS 15导航栏进行单独适配。
-
新代码
if #available(iOS 15, *) { let barApp = UINavigationBarAppearance.init() barApp.configureWithOpaqueBackground() barApp.titleTextAttributes = [ NSAttributedString.Key.font : UIFont.systemFont(ofSize: 18.0), NSAttributedString.Key.foregroundColor : UIColor.white ] barApp.backgroundColor = .red navigationBar.scrollEdgeAppearance = barApp navigationBar.standardAppearance = barApp }
2. UITabBar
UITabBar的问题和UINavigationBar属于同一类,都是部分属性失效
-
旧代码
let tabBar: UITabBar = UITabBar.init() tabBar.backgroundColor = .red let tabBarItem: UITabBarItem = UITabBarItem.init() let norAttributes = [ NSAttributedString.Key.font : UIFont.systemFont(ofSize: 18.0), NSAttributedString.Key.foregroundColor : UIColor.white ] let selectedAttributes = [ NSAttributedString.Key.font : UIFont.systemFont(ofSize: 18.0), NSAttributedString.Key.foregroundColor : UIColor.black ] tabBarItem.setTitleTextAttributes(norAttributes, for: .normal) tabBarItem.setTitleTextAttributes(selectedAttributes, for: .selected)
同样的UITabBar也需要用UITabBarAppearance来设置属性
-
新代码
if #available(iOS 15, *) { let tabBar: UITabBar = UITabBar.init() let tabBarApp = UITabBarAppearance.init() tabBarApp.backgroundColor = .red let norAttributes = [ NSAttributedString.Key.font : UIFont.systemFont(ofSize: 18.0), NSAttributedString.Key.foregroundColor : UIColor.white ] let selectedAttributes = [ NSAttributedString.Key.font : UIFont.systemFont(ofSize: 18.0), NSAttributedString.Key.foregroundColor : UIColor.black ] tabBarApp.stackedLayoutAppearance.normal.titleTextAttributes = norAttributes tabBarApp.stackedLayoutAppearance.selected.titleTextAttributes = selectedAttributes tabBar.scrollEdgeAppearance = tabBarApp tabBar.standardAppearance = tabBarApp }
3. UIImage
UIImage新增了几个调整尺寸的方法
UIImage(named: "")?.preparingThumbnail(of: CGSize.init(width: 100, height: 100))
// 闭包中直接获取调整后的UIImage(如果失败将返回nil)
UIImage(named: "")?.prepareThumbnail(of: CGSize.init(width: 200, height: 200), completionHandler: { image in
// 处理图片
})
await UIImage(named: "")?.byPreparingThumbnail(ofSize: CGSize.init(width: 300, height: 300))
4. UITableView
iOS 15对 UITableView新增了sectionHeaderTopPadding作为列表每个部分标题上方的填充,并且它的默认值是UITableViewAutomaticDimension(高度为22),所以我们要将它的值设置为0,否则TableView section header的高度会增加22像素,适配代码:
if #available(iOS 15, *) {
tableView.sectionHeaderTopPadding = 0
}
5. UIButton新增属性
UIButton支持更多配置,UIButton.configuration是一个新的结构体,它指定按钮以及其内容的外观和行为。如cornerStyle、baseForegroundColor、baseBackgroundColor、buttonSize、title、image、subtitle、titlePadding、imagePadding、contentInsets、imagePlacement等。
let plain: UIButton = UIButton(configuration: .plain(), primaryAction: nil)
plain.setTitle("plain", for: .normal)
let tinted: UIButton = UIButton(configuration: .tinted(), primaryAction: nil)
tinted.setTitle("tinted", for: .normal)
let filled: UIButton = UIButton(configuration: .filled(), primaryAction: nil)
filled.setTitle("filled", for: .normal)