一、UINavigationBar與UITabBar在頁面內容(ScrollView)沒有與其相交時為透明狀態
iOS 15開始用UINavigationBarAppearance和UITabBarAppearance設置UINavigationBar與UITabBar,UINavigationBar與UITabBar都有scrollEdgeAppearance和standardAppearance兩個屬性,scrollEdgeAppearance控制頁面沒有與其相交的狀態,standardAppearance控制頁面內容與其相交的狀態,如下設置不透明純色的UINavigationBar與UITabBar:
navigationBar.tintColor = UIColor.white navigationBar.isTranslucent = false if #available(iOS 15.0, *) { let appearance = UINavigationBarAppearance() appearance.configureWithOpaqueBackground() appearance.backgroundColor = UIColor.blue appearance.titleTextAttributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16.5) as Any, NSAttributedString.Key.foregroundColor: UIColor.white] appearance.largeTitleTextAttributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16.5) as Any, NSAttributedString.Key.foregroundColor: UIColor.white] appearance.backgroundImage = UIImage() appearance.shadowImage = UIImage() navigationBar.standardAppearance = appearance navigationBar.scrollEdgeAppearance = appearance } else { navigationBar.barTintColor = UIColor.blue navigationBar.titleTextAttributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16.5) as Any, NSAttributedString.Key.foregroundColor: UIColor.white] navigationBar.setBackgroundImage(UIImage(), for: .default) navigationBar.shadowImage = UIImage() }
tabBar.isTranslucent = false if #available(iOS 15.0, *) { let appearance: UITabBarAppearance = UITabBarAppearance() appearance.configureWithOpaqueBackground() appearance.backgroundImage = UIImage() appearance.shadowImage = getImage(color: .gray) appearance.backgroundColor = .white tabBar.standardAppearance = appearance tabBar.scrollEdgeAppearance = appearance } else { tabBar.backgroundImage = UIImage() tabBar.shadowImage = getImage(color: .gray) tabBar.tintColor = .blue tabBar.barTintColor = .white }
/// 用UIColor生成UIImage func getImage(color:UIColor, rect: CGRect = CGRect(x: 0, y: 0, width: 1.0, height: 1.0))->UIImage{ UIGraphicsBeginImageContext(rect.size) let context:CGContext = UIGraphicsGetCurrentContext()! context.setFillColor(color.cgColor) context.fill(rect) let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()! UIGraphicsEndImageContext() return image }
可以在viewController中設置UITabBarItem的樣式:
let font = UIFont.systemFont(ofSize: 10) if #available(iOS 15.0, *) { let appearance: UITabBarAppearance = tabBar.standardAppearance appearance.stackedLayoutAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.gray,NSAttributedString.Key.font: font] appearance.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.blue,NSAttributedString.Key.font: font] appearance.stackedLayoutAppearance.normal.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -2) appearance.stackedLayoutAppearance.selected.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -2) vc.tabBarItem.standardAppearance = appearance vc.tabBarItem.scrollEdgeAppearance = appearance } else { vc.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.gray,NSAttributedString.Key.font: font], for: .normal) vc.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.blue,NSAttributedString.Key.font: font], for: .highlighted) vc.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -2) }
具體可參考外網彼得潘的文章(需FQ):
https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/ios-15-navigation-bar-tab-bar-%E7%9A%84%E6%A8%A3%E5%BC%8F%E8%A8%AD%E5%AE%9A-558f07137b52
這是對這一iOS 15特性以及appearance描述較詳細易懂的文章。
對於UINavigationBar為白底黑字的app,這一特性可能影響不大,看情況適配。
二、UILabel顯示的文字比設置font小
在iOS 15中,UILabel 設置adjustsFontSizeToFitWidth為true時,高度不能跟設置的 font 一樣大(具體需要大多少沒研究,加大到能正常顯示就行)。
三、固定寬度的UILabel文字顯示不全
在iOS 15以下計算的正好顯示完UILabel文字的寬度,在iOS 15上可能顯示不全,增加寬度即可。
四、UITableView 設置 tableHeaderView 后,底部會增加一段高度
如果UITableView設置了tableHeaderView而沒有設置tableFooterView,可能會出現這個問題,不確定是否是beta版的bug,解決方案是在設置tableHeaderView時同時設置一個高度為0.01的tableFooterView即可:
tableView?.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 0.01))
設置這句代碼后,之后在把tableFooterView設置為nil或者其他UIView都不影響。
五、plain類型的UITableView增加默認的section高度
UITableView新增了一個新屬性:sectionHeaderTopPadding,默認值為automaticDimension,當我們使用UITableViewStylePlain 初始化tableView的時候,sectionHeaderTopPadding會給section header默認增加高度,解決方案是把sectionHeaderTopPadding屬性設置為0即可:
if #available(iOS 15.0, *) { tableView?.sectionHeaderTopPadding = 0 }
六、使用layoutIfNeeded更新tableHeaderView的frame沒有生效
使用StoryBoard搭建tableView,tableHeaderView的需要適配屏幕尺寸而呈現不同的高度,此時改變tableHeaderView的frame並調用layoutIfNeeded方法不會生效,tableHeaderView的高度雖然改變,但是UITableView的布局不變,解決方案是重新給tableHeaderView賦值:
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) { if let headerView = self.tableView?.tableHeaderView, let lastSubView = headerView.subviews.last { if #available(iOS 15.0, *) { headerView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: lastSubView.frame.origin.y + lastSubView.frame.size.height) self.tableView?.tableHeaderView = headerView } else { self.tableView?.tableHeaderView?.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: lastSubView.frame.origin.y + lastSubView.frame.size.height) self.tableView?.tableHeaderView?.layoutIfNeeded() } } }
七、plain類型的UITableView默認不顯示灰色section header
上滑置頂時最上面的section header才顯示顏色,對於部分頁面可能出現UI上的bug,解決方案是用viewForHeaderInSection代理方法返回帶顏色的section header:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let sectionHeaderView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 20)) sectionHeaderView.backgroundColor = UIColor.gray return sectionHeaderView }
八、 特殊端口(如10080)的網址在WKWebView中顯示空白頁about:blank
更新iOS 15.0后,特殊端口(如10080)的網址在WKWebView中顯示空白頁about:blank,iOS 15.0 的Safari瀏覽器以及最新版Google Chrome瀏覽器均無法顯示,Google Chrome顯示ERR_UNSAFE_PORT。
原因:為了防止 NAT Slipstream 2.0 attack,各大瀏覽器均在新版或測試版中禁用包括10080在內的不安全端口號。
解決方案:很快 10080 端口將全網無法使用,仍使用該端口號的網站應該盡快修改端口號。
參考:https://discussionschinese.apple.com/thread/252989984
九、Facebook的跨平台框架React中的fishhook閃退
解決方案是修改fishhook.c源碼,參考https://github.com/facebook/fishhook/issues/85 中的XuweiQT的方案,將鏈接中的fishhook.c替換項目中fishhook.c文件。