Determine selected text color to use in Mac Catalyst for UITableView Cells When Row is Selected But Table View Doesn't Have Active Focus?

So when a row is selected in UITableView and the table view / cells within it have focus, and the window is active.... the rows are selected with the system Accent color (in my case blue).

I have a view inside the table view cell that does custom drawing. When the cell is selected I draw text white (when unselected I draw the text using UIColor.labelColor).

So when the window is not active but the table view row is selected, the selected background color changes to light gray. In this case I draw the text black (because it looks better against the light gray selection color on an inactive window).

However when the row is selected but another view in the window has focus (say the sidebar) the selected row turns back to the light gray color. In this case my text is incorrectly drawing white instead of black on the light gray background. How do I check for this state from UIKit? In AppKit I think I'd be using:

NSColor.unemphasizedSelectedTextColor https://developer.apple.com/documentation/appkit/nscolor/2998834-unemphasizedselectedtextcolor?language=objc

I have to update text color manually because I'm doing custom drawing in these table view cells (I have a view that overrides drawRect:) I can't just use the dynamic colors like UIColor.labelColor. I have to set the colors on state change manually and call setNeedsDisplay on my view.

How do I check for the unemphasized state? I did inspect whether or not my table view cell is in the active focusItem's environment, and if it is not I assume the light gray selected color is being used. This worked most of the time but there still seems to be some states where the unemphasized selection color (light gray)is being used on selected cells when the the table view cell is in the active focus environment and my text incorrectly draws white.

So the "unemphasized" state happens even if the selected rows are in the active focus environment if a table view cell is swiped. There doesn't seem to be anything in the UICellConfigurationState to indicate this state, nor the trait collection.

I don't see any properties that make it easy to detect for this "unemphasized" state. The workaround I came up with (limited testing) is to inspect the background color on the background configuration directly and compare it against the default tint color when the window is active....(using -traitCollectionWithActiveAppearance: and -performAsCurrent:). Code is pretty ugly.

Determine selected text color to use in Mac Catalyst for UITableView Cells When Row is Selected But Table View Doesn't Have Active Focus?
 
 
Q