我的wanandroid客户端简单适配iOS 15

2,397 阅读3分钟

小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。

前言

中秋小长假之后来上班,我就发了一天沸点吐槽自己一天的工作日常:

image.png

讲真,每年的9月可能是iOS开发比较苦恼的日子,除了对应日常功能迭代与bug修复,还要针对新版iOS做适配工作。

我自己的wanandroid客户端项目,页面和逻辑相比较很简单,所以做适配比较快,给大伙分享一下。

NavigationBarTabBar都变透明了

代码一跑,首页就沦陷了,怎么NavigationBar和TabBar都变透明了?这里为了方便UI观察,所以用的Light模式。

IMG_851D65461BD7-1.jpeg

立马去查了问题开始修改:

private func iOS15NavigationBarAndTabBarClearFix() {

    if #available(iOS 15.0, *) {
        let navBarAppearance = UINavigationBarAppearance()
        navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance

        let tabBarAppearance = UITabBarAppearance()
        tabBarController?.tabBar.scrollEdgeAppearance = tabBarAppearance

    }
}

由于是开源项目,我对于NavigationBarTabBar都没有做过多的定制化需求,都是使用默认的,所以这里对navBarAppearancetabBarAppearance页面没有做特别多的配置化,只是分别赋值给navigationBar.scrollEdgeAppearancetabBar.scrollEdgeAppearance就完事了。

倒是这个scrollEdgeAppearance以及相关的属性,看了半天注释也没让人太明白,有大佬能指点一二感激不尽:

/*

     Fallback Behavior:
     1) Appearance objects are used in whole – that is, all values will be sourced entirely from an
     instance of UINavigationBarAppearance defined by one of these named properties (standardAppearance,
     compactAppearance, scrollEdgeAppearance, compactScrollEdgeAppearance) on either UINavigationBar
     (self) or UINavigationItem (self.topItem).
     2) The navigation bar will always attempt to use the most relevant appearance instances first,
     before falling back to less relevant ones. The fallback logic is:
         AtScrollEdge: self.topItem.scrollEdgeAppearance => self.scrollEdgeAppearance
             => self.topItem.standardAppearance => self.standardAppearance
         AtCompactScrollEdge: self.topItem.compactScrollEdgeAppearance
             => self.compactScrollEdgeAppearance => self.topItem.scrollEdgeAppearance
             => self.scrollEdgeAppearance => self.topItem.compactAppearance
             => self.compactAppearance => self.topItem.standardAppearance => self.standardAppearance
         CompactSize: self.topItem.compactAppearance => self.compactAppearance
             => self.topItem.standardAppearance => self.standardAppearance
         NormalSize: self.topItem.standardAppearance => self.standardAppearance

     */

  
    /// Describes the appearance attributes for the navigation bar to use when it is displayed with its standard height.

    @available(iOS 13.0, *)
    @NSCopying open var standardAppearance: UINavigationBarAppearance

    /// Describes the appearance attributes for the navigation bar to use when it is displayed with its compact height. If not set, the standardAppearance will be used instead.
    
    @available(iOS 13.0, *)
    @NSCopying open var compactAppearance: UINavigationBarAppearance?

    /// Describes the appearance attributes for the navigation bar to use when an associated UIScrollView has reached the edge abutting the bar (the top edge for the navigation bar). If not set, a modified standardAppearance will be used instead.

    @available(iOS 13.0, *)
    @NSCopying open var scrollEdgeAppearance: UINavigationBarAppearance?

    /// Describes the appearance attributes for the navigation bar to use when it is displayed with its compact heights, and an associated UIScrollView has reached the edge abutting the bar. If not set, first the scrollEdgeAppearance will be tried, and if that is nil then compactAppearance followed by a modified standardAppearance.

    @available(iOS 15.0, *)
    @NSCopying open var compactScrollEdgeAppearance: UINavigationBarAppearance?

这是改完后的效果图:

IMG_01E884D94464-1.jpeg

UITableView的sectionHeaderView变高了

看见开发环境这个地方,顶部空了好大一截没有,就是这个问题:

IMG_C70E5EFD4087-1.jpeg

而问题的元凶出在这里,下面代码引自UITableView的源码:

/// Padding above each section header. The default value is `UITableViewAutomaticDimension`.

    @available(iOS 15.0, *)
    open var sectionHeaderTopPadding: CGFloat

iOS 15中多了一个sectionHeaderTopPadding属性,而默认高度是UITableViewAutomaticDimension

这个情况下,只用把sectionHeaderTopPadding设置为0即可:

if #available(iOS 15.0, *) {
    tableView.sectionHeaderTopPadding = 0
}

把这段代码放到需要使用TableView的SectionHeaderView的位置就好了,修改后如下:

IMG_15EFCA27E1DB-1.jpeg

参考文档

iOS15适配

总结

针对iOS 15适配,我的wanandroid客户端就只有这么一点,当然每个App的复杂程度不同,面临的困难也不同。

我个人的经验如下:

  • 写好基类,有些问题在基类改完了,所有页面自然就好,不必一个一个页面去改造。论写基类的重要性。

  • 先改好,再弄清原因,用户可不会理解你那么多。

  • 分清问题的主次,先解决会导致崩溃,极度影响体验的问题,其他问题可以慢慢改。

  • 耐心,适配出现的问题,肯定不是一家,过一段时间,网上自然回有各种解决方法。

我们下期见。

目前RxSwift编写wanandroid客户端已经开源了——项目链接

现已适配iOS 15。

记得给个star喔!

附上一张效果图片:

RPReplay_Final1627969241.2021-08-03 13_44_57.gif