xyk blog

最近は iOS 開発の記事が多めです。

iOS13 で UITableViewCell をタップしても背景色がハイライトしなくなった

検証環境:
Xcode 11.1
Swift 5.1

iOS13 で UITableViewCell をタップしてもハイライトしなくなったケースがあった。
原因はこちらの UIKit の仕様変更だった。

Apple Developer Documentation

The UITableViewCell class no longer changes the backgroundColor or isOpaque properties of the contentView and any of its subviews when cells become highlighted or selected. If you are setting an opaque backgroundColor on any subviews of the cell inside (and including) the contentView, the appearance when the cell becomes highlighted or selected might be affected. The simplest way to resolve any issues with your subviews is to ensure their backgroundColor is set to nil or clear, and their opaque property is false. However, if needed you can override the setHighlighted(:animated:) and setSelected(:animated:) methods to manually change these properties on your subviews when moving to or from the highlighted and selected states. (13955336)

UITableViewCell 選択時に contentView 及びそのサブビューの backgroundColor または isOpaque プロパティが変更されなくなり、 contentView 及びそのサブビューに不透明な backgroundColor を設定している場合には影響を受けてハイライト表示されなくなる。

これを解決するには ハイライトさせたいビューの backgroundColornil または clear に設定し、isOpaque プロパティを false にすればよいとのこと。

なんだけど isOpaque プロパティは true のままでも backgroundColornil または clear にすればハイライトするようになった。