iOS14 适配 UITableViewHeaderFooter
2020-09-17 本文已影响0人
Jason_li012
今天,迫不及待把升级到Xcode12 和手机系统更新到iOS14 ,结果跑了项目,发现在我的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
在系统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.
}
却发现iOS14增加了backgroundConfiguration 配置方法。
backgroundConfiguration: 背景配置为您提供了一种轻巧的方式来为视图创建背景。 使用背景配置,可以获得各种不同视图状态的系统默认背景样式。 您可以将后台配置直接应用于UICollectionView和UITableView中的单元格,头部View和 底部View。
所以把UITableViewHeaderFooterView 中的backgroundConfiguration 设置为通明配置,没有默认样式就行了。
if #available(iOS 14.0, *) {
self.backgroundConfiguration = UIBackgroundConfiguration.clear()
} else {
// Fallback on earlier versions
}