Text color in UIMenuController affected by UIButton appearance setting

拜拜、爱过 提交于 2019-12-05 02:24:46

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];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!