【问题标题】:How to change button text color of UIAlertView in iOS7?如何在 iOS7 中更改 UIAlertView 的按钮文本颜色?
【发布时间】:2013-10-12 04:03:00
【问题描述】:

iOS7 引入了通用的 tint-color。我认为UIAlertView 也在有效范围内,但实际上 tintColor 看起来不适用于UIAlertView。 (用于可点击的按钮文本颜色)

是否可以更改警报视图的色调?可以的话怎么改?

【问题讨论】:

  • 试试看PMAlertController 一个小库,它允许您用漂亮且完全可自定义的警报替换 Apple 不可自定义的 UIAlertController。

标签: ios7 uialertview tintcolor


【解决方案1】:

之前提到[[UIView appearance] setTintColor:[UIColor redColor]]; 的答案应该可以工作,但是如果您只想更改UIAlertView 色调颜色,那么这段代码的作用要比应该做的多。

您可以通过这种方式在 iOS 上瞄准组件:

[[UIView appearanceWhenContainedIn:[UIAlertView class], nil] setTintColor:[UIColor redColor]];
[[UIView appearanceWhenContainedIn:[UIAlertController class], nil] setTintColor:[UIColor redColor]];

以及在 iOS >9 上:

[[UIView appearanceWhenContainedInInstancesOfClasses:@[[UIAlertView class]]] setTintColor:[UIColor redColor]];
[[UIView appearanceWhenContainedInInstancesOfClasses:@[[UIAlertController class]]] setTintColor:[UIColor redColor]];

【讨论】:

    【解决方案2】:

    UIActionSheet也可以通过这种方式改变按钮的颜色。

    [[UIView appearance] setTintColor:[UIColor redColor]];
    

    【讨论】:

    • “这样”是什么意思?
    • [[UIView appearance] setTintColor:[UIColor redColor]]; 也可以临时更改UIActionSheet 按钮的颜色
    • @answer-huang 我将您的评论中的“这种方式”复制到您的答案中。
    【解决方案3】:

    很遗憾,您无法自定义警报视图的外观,无法更改按钮文本颜色。

    UIAlertView Class Reference中明确提到:

    UIAlertView 类旨在按原样使用,而不是 支持子类化。此类的视图层次结构是私有的,并且 不得修改。

    更新:

    问题是关于 iOS7,但现在 UIAlertView 已弃用,对于 UIAlertController,您可以简单地更改视图色调:

        let alert = UIAlertController(title: "Gimme a break",
                                    message: "For God sake its iOS7 question!",
                             preferredStyle: .alert)
        alert.view.tintColor = UIColor.red
    

    检查这个问题也很有帮助:How to change UIAlertController button text colour in iOS9?

    【讨论】:

    • @downvoter 而不是这个懦弱的downvote,请告诉我们是否使用UIAlertView 来实现这一目标!
    • -(void)willPresentAlertView:(UIAlertView *)alertView 应该为您提供编辑所有子视图的选项,并且不可避免地取消按钮是该“alertview”对象的子视图。虽然找不到..
    • 不应该。但是你可以
    • 刚刚遇到这个,它是如此正确,所以+1。至于其他两个 cmets,@eiran 只是 NO 非常非常糟糕的建议您不应该 弄乱UIAlertView 的视图层次结构,这在文档中非常清楚.这样做是让您的申请被 Apple 拒绝的快速通道。至于WouterDS 你可以添加子视图的唯一原因是因为UIAlertViewUIView 的子类,它允许视图层次结构被弄乱,所以你可以不应该跨度>
    • 这是可能的:[[UIView appearance] setTintColor:[UIColor redColor]]; 但是要知道,这也会为所有其他可着色视图着色。
    猜你喜欢
    • 1970-01-01
    • 2014-08-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    • 1970-01-01
    • 2012-06-25
    • 1970-01-01
    相关资源
    最近更新 更多