Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell gaves default blue highlight after second-third selection for .gray on macOS using Catalyst

I trying to port my iOS app to macOS using Catalyst, but no matter what the UITableViewCellSelectionStyle.gray style visually show the .blue highlight, while the .style value still prints .gray. I tried to force reset it in the following functions but with no luck.

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath?

No matter what, it goes back .blue from .gray. If its .none, it stays .none.

Possible workaround was to set a background color manually, which feels like not right.

like image 620
Laszlo Avatar asked Oct 12 '25 15:10

Laszlo


2 Answers

The blue color was from the focused state, so you will need to override the canBecomeFocused property in your UITableViewCell subclass

override var canBecomeFocused: Bool {
    return false
}
like image 109
James Tang Avatar answered Oct 14 '25 06:10

James Tang


The default way of setting the selection style on Mac Catalyst did not work for me:

cell.selectionStyle = .gray

On the Mac Catalyst app the selection showed first gray then on the next click blue. To work around it I applied @Laszlo's solution. However you don't need a custom UITableViewCell. You can create a custom background view and set the .selectedBackgroundView property of the UITableViewCell instance to a custom background view.

let customSelectionBackgroundView = UIView()
customSelectionBackgroundView.backgroundColor = UIColor.red
cell.selectedBackgroundView = customSelectionBackgroundView
like image 44
a.ajwani Avatar answered Oct 14 '25 04:10

a.ajwani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!