iOS 15 beta适配笔记

一、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文件。

 

posted @ 2021-08-10 17:08  Naruto_L  阅读(2444)  评论(1编辑  收藏  举报