What UIKit Object controls “Look Up" popup appearance?

Viewed 159

In our app, we have a light mode and a dark mode that switch the text colors and background colors. Theme is an enum with cases light and dark. We have methods for switching the colors, such as:

var textColor: UIColor {
    switch self {
    case .light:
        return UIColor(red:60/255.0, green:60/255.0, blue:60/255.0, alpha: 1.0)
    case .dark:
        return UIColor(red: 240.0/255.0, green: 248.0/255.0, blue: 255.0/255.0, alpha: 1.0)
    }
}

And we have an apply method that applies the Theme to all of the views we use in our app:

func apply(){
    defaults.set(rawValue, forKey: "selectedTheme")

    UIApplication.shared.delegate?.window??.tintColor = tintColor

    let navBarAppearance = UINavigationBar.appearance()
    navBarAppearance.barStyle = barStyle
    navBarAppearance.backgroundColor = backgroundColor
    navBarAppearance.barTintColor = backgroundColor
    navBarAppearance.tintColor = tintColor
    navBarAppearance.isTranslucent = false
    navBarAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: textColor] 
    let tabBarAppearance = UITabBar.appearance()
    tabBarAppearance.barStyle = barStyle
    tabBarAppearance.backgroundColor = backgroundColor
    tabBarAppearance.barTintColor = backgroundColor

    let toolBar = UIToolbar.appearance()
    toolBar.backgroundColor = backgroundColor
    toolBar.tintColor = backgroundColor
    toolBar.barStyle = barStyle
    toolBar.isTranslucent = false
    toolBar.barTintColor = backgroundColor

    UITableViewCell.appearance().backgroundColor = backgroundColor
    UILabel.appearance(whenContainedInInstancesOf: [UITableViewCell.self]).textColor = textColor
    UILabel.appearance().textColor = textColor

    UITextField.appearance().keyboardAppearance = keyboardColor
    UITextField.appearance().textColor = textColor
    UITextField.appearance().backgroundColor = backgroundColor

    UITextView.appearance().textColor = textColor
    UITextView.appearance().backgroundColor = backgroundColor

    MultiSelectSegmentedControl.appearance().tintColor = tintColor
    MultiSelectSegmentedControl.appearance().backgroundColor = backgroundColor

    UISegmentedControl.appearance().tintColor = tintColor
    UISegmentedControl.appearance().backgroundColor = backgroundColor

    UIButton.appearance().tintColor = tintColor

    BigButton.appearance().backgroundColor = Theme.current.tintColor
    BigButton.appearance().tintColor = Theme.current.backgroundColor

    UIPickerView.appearance().backgroundColor = Theme.current.backgroundColor
    UIPickerView.appearance().tintColor = Theme.current.tintColor

    UITableView.appearance().backgroundColor = backgroundColor
    UITableView.appearance().separatorColor = cellSelectionColor

    UISearchBar.appearance().backgroundColor = backgroundColor
    UISearchBar.appearance().barTintColor = tintColor
    UISearchBar.appearance().searchBarStyle = .minimal
    UITextView.appearance().backgroundColor = backgroundColor
    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).textColor = textColor

    UISwitch.appearance().onTintColor = tintColor

    let colorView = UIView()
    colorView.backgroundColor = cellSelectionColor

    UITableViewCell.appearance().selectedBackgroundView = colorView
}

When one selects text in our app and presses the "look up" button in the popup menu item, the popup that appears looks right. However, when you click on some of the views (such as dictionary and siri knowledge), the background or text colors appear to have been changed. Example:

Darkened Text

How can we control the text color on this popup?

EDIT: The goal is to find the name of the UI Object that is created when the UIMenuItems are pressed. It’s not in the XCode Debugger Inspector, nor is it in self.view.subviews. It seems to be some kind of built-in object that is hidden from the rest of the processes.

1 Answers

There could be one or two possible issues

1) You need to make sure that you are not setting textColor for labels and backgroundColor for the view explicitly in the code. Label's color should be kept default so that it can use the appearance color. For example, lets say we have set the textColor as default as shown in the below image,

enter image description here

Now if we apply the textColor appearance on UILabel by doing UILabel.appearance().textColor = .green. It will change the color to green once you will open the popup.

But if you are setting the textColor explicitly after setting the appearance as below,

UILabel.appearance().textColor = .green
copyrightLabel.textColor = .yellow

then it will discard the appearance setting and set the color(i.e, .yellow) you provided later.

2) The other possible issue could be the creation of this popup before changing the appearance. What i mean is if you have instantiated this popup at the time when appearance was light so if you change the appearance now to dark, it will not update the popup colors automatically to dark because it's already created. If this is the case then you might have to do reloadData on tableView/collectionView and update all other view colors yourself.

Related