By setting the Titlecolor of a UIButton
with appearance, the UIMenuItems
in a UIMenuController
of a UITextView
are getting the same color.
applicationDidFinishLaunching
:[[UIButton appearance] setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
Is there a way to suppress it
or
give a UIMenuItems
another color?
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.
With appearanceWhenContainedIn UIMenuController/UIMenuItem
Is not possible, since both are not implementing the UIAppearanceContainer protocol.
I found 2 ways to fix this issue.
Here is a screenshot of the result of the following solutions :
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.
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With