iOS14 適配 UITableViewHeaderFooterView 設置透明背景無效

今天,迫不及待把升級到Xcode12 和手機系統(tǒng)更新到iOS14 ,結果跑了項目,發(fā)現(xiàn)在我的UITableView 列表中,一個UITableViewHeaderFooterView 背景設置為通明之后,有一個毛玻璃顏色。
情景: 頁面是有一個backView 和 UITableView 組成,backView放在底部,是有CAGradientLayer 畫了一個半圓,而UITableView 的HeaderView 需要設置通明色,來顯示背景半圓效果。如圖:


WechatIMG7.png

然而,升級到iOS14之后成了這樣子


WechatIMG11.jpeg

然后使用debug view hierarchy 看出來HeaderView 層級有所變化,
WechatIMG8.jpeg
old : ProbabilityCollegeHeaderView -> _UISystemBackgroundView -> _UITableViewHeaderFooterContentView
new : ProbabilityCollegeHeaderView -> _UISystemBackgroundView -> UIView -> _UITableViewHeaderFooterContentView

在系統(tǒng)View 嵌套里面多了一個UIView,并且Api方法里面沒有找到這個屬性

@available(iOS 6.0, *)
open class UITableViewHeaderFooterView : UIView {

    public init(reuseIdentifier: String?)

    public init?(coder: NSCoder)

    /// Requests the view update its configuration for its current state. This method is called automatically
    /// when the view's `configurationState` may have changed, as well as in other circumstances where an
    /// update may be required. Multiple requests may be coalesced into a single update at the appropriate time.
    @available(iOS 14.0, *)
    open func setNeedsUpdateConfiguration()

    /// When YES, the header/footer will automatically call -updatedConfigurationForState: on its `contentConfiguration` when the header/footer's
    /// configuration state changes, and apply the updated configuration back to the header/footer. The default value is YES.
    @available(iOS 14.0, *)
    open var automaticallyUpdatesContentConfiguration: Bool
    open var contentView: UIView { get }

    // These properties will always return nil when a non-nil `contentConfiguration` is set.
    @available(iOS, introduced: 6.0, deprecated: 100000, message: "Use UIListContentConfiguration instead, this property will be deprecated in a future release.")
    open var textLabel: UILabel? { get }

    @available(iOS, introduced: 6.0, deprecated: 100000, message: "Use UIListContentConfiguration instead, this property will be deprecated in a future release.")
    open var detailTextLabel: UILabel? { get } // only supported for headers in grouped style

    /// When YES, the header/footer will automatically call -updatedConfigurationForState: on its `backgroundConfiguration` when the header/footer's
    /// configuration state changes, and apply the updated configuration back to the header/footer. The default value is YES.
    @available(iOS 14.0, *)
    open var automaticallyUpdatesBackgroundConfiguration: Bool

    // This property is always nil when a non-nil `backgroundConfiguration` is set.
    open var backgroundView: UIView?
    open var reuseIdentifier: String? { get }
    open func prepareForReuse() // if the view is reusable (has a reuse identifier), this is called just before the view is returned from the table view method dequeueReusableHeaderFooterViewWithIdentifier:.  If you override, you MUST call super.
}

卻發(fā)現(xiàn)iOS14增加了backgroundConfiguration 配置方法。
backgroundConfiguration: 背景配置為您提供了一種輕巧的方式來為視圖創(chuàng)建背景。 使用背景配置,可以獲得各種不同視圖狀態(tài)的系統(tǒng)默認背景樣式。 您可以將后臺配置直接應用于UICollectionView和UITableView中的單元格,頭部View和 底部View。
所以把UITableViewHeaderFooterView 中的backgroundConfiguration 設置為通明配置,沒有默認樣式就行了。

        if #available(iOS 14.0, *) {
            self.backgroundConfiguration = UIBackgroundConfiguration.clear()
        } else {
            // Fallback on earlier versions
        }
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容