UIMenuController中的文本颜色受UIButton外观设置的影响 [英] Text color in UIMenuController affected by UIButton appearance setting

查看:104
本文介绍了UIMenuController中的文本颜色受UIButton外观设置的影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过设置UIButton Titlecolor 的外观,UITextViewUIMenuController中的UIMenuItems获得相同的颜色.

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

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

我的问题:

有没有办法抑制它

UIMenuItems提供另一种颜色?

My question:

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

  1. 具有外观WhenContainedIn UITextview
    我尝试使用
    设置TextViews中包含的按钮的外观 [UIButton appearanceWhenContainedIn:[UITextView class], nil]
    但这显然不起作用,因为UIMenuController不在TextView内.

  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.

具有外观WhenContainedIn UIMenuController/UIMenuItem
这是不可能的,因为两者都没有实现UIAppearanceContainer协议.

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

推荐答案

我发现了解决此问题的2种方法.

I found 2 ways to fix this issue.

以下是以下解决方案结果的屏幕截图:

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

UIMenuController不包含在View Controller视图层次结构中.因此,您可以通过以下方式定义UIButton颜色(代替设置全局Button 外观):

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) :

快捷键:

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.

第二种解决方案直接使用Apple在菜单控制器中使用的私有UIButton子类类名. 我从不建议访问私有的Apple类(并且通过其名称),但是在那种特定的Menu Controller颜色自定义情况下,我认为这是最好的解决方案.它使您可以定义视图外观的简洁方式.

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.

快捷键:

定义全局Button标题的颜色外观:

Define your global Button title color appearance :

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

MenuController的特定异常:

Specific exception for the MenuController :

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

Objective-C:

定义全局Button标题的颜色外观:

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

MenuController的特定异常:

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

这篇关于UIMenuController中的文本颜色受UIButton外观设置的影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆