-->

Text color in UIMenuController affected by UIButto

2019-04-21 08:44发布

问题:

I've observed following:

By setting the Titlecolor of a UIButton with appearance, the UIMenuItems in a UIMenuController of a UITextView are getting the same color.

Code in applicationDidFinishLaunching:

[[UIButton appearance] setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];  

My question:

Is there a way to suppress it
or
give a UIMenuItems another color?

What i have tried:

  1. With appearanceWhenContainedIn UITextview
    I've tried to set the appearance for buttons contained in TextViews with
    [UIButton appearanceWhenContainedIn:[UITextView class], nil]
    But this obviously didn't work since the UIMenuController is not inside the TextView.

  2. With appearanceWhenContainedIn UIMenuController/UIMenuItem
    Is not possible, since both are not implementing the UIAppearanceContainer protocol.

回答1:

I found 2 ways to fix this issue.

Here is a screenshot of the result of the following solutions :

First solution

The UIMenuController is not contained in the View Controller views hierarchy. You can thus define your UIButton color that way (instead of setting the global Button appearance) :

Swift :

UIButton.appearanceWhenContainedInInstancesOfClasses([UIViewController.self]).setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)

Objective-C :

[[UIButton appearanceWhenContainedInInstancesOfClasses:@[UIViewController.class]] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

That solution works for most of the cases. But if you use the delete button or actions button of Table View Cells it will also take the set color and you won't be able to change that color through appearance proxy.

Second solution (my preferred one)

The second solution uses directly the private UIButton subclass class name used by Apple in the Menu Controller. I would never recommend to access a private Apple class (and furthermore through its name), but in that specific Menu Controller color customization case I think that's the best solution. It lets you define the clean way your view appearances.

Swift :

Define your global Button title color appearance :

UIButton.appearance().setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)

Specific exception for the MenuController :

(NSClassFromString("UICalloutBarButton")! as! UIButton.Type).appearance().setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)

Objective-C :

Define your global Button title color appearance :

[[UIButton appearance] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

Specific exception for the MenuController :

[[NSClassFromString(@"UICalloutBarButton") appearance] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];