Text color in UIMenuController affected by UIButton appearance setting
Asked Answered
T

1

11

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.

Telemark answered 20/10, 2014 at 9:5 Comment(3)
See also: #5467070Delrosario
I'm having the exact same problem. Have you discovered a solution?Hayse
No i could not fix it. :/Telemark
F
6

I found 2 ways to fix this issue.

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

enter image description here

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];
Fsh answered 25/2, 2016 at 13:34 Comment(1)
UIButton.appearance().setTitleColor doesn't seem valid codeMackle

© 2022 - 2024 — McMap. All rights reserved.