【问题标题】:Change text color in UIActionSheet buttons更改 UIActionSheet 按钮中的文本颜色
【发布时间】:2013-04-23 07:44:52
【问题描述】:

在我的项目中,我使用UIActionSheet 来显示某些排序类型。我想更改操作表按钮中显示的文本的颜色。我们如何改变文本颜色?

【问题讨论】:

    标签: ios uiactionsheet


    【解决方案1】:

    iOS 8: UIActionSheet 已弃用。 UIAlertController 尊重 -[UIView tintColor],所以这行得通:

    alertController.view.tintColor = [UIColor redColor];
    

    更好的是,在您的应用程序委托中设置整个窗口的色调:

    self.window.tintColor = [UIColor redColor];
    

    iOS 7:在您的操作表委托中,实现-willPresentActionSheet:,如下所示:

    - (void)willPresentActionSheet:(UIActionSheet *)actionSheet
    {
        for (UIView *subview in actionSheet.subviews) {
            if ([subview isKindOfClass:[UIButton class]]) {
                UIButton *button = (UIButton *)subview;
                [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            }
        }
    }
    

    【讨论】:

    • 这仅适用于默认状态。如果您点击按钮,它会在 titleLabel 上保持蓝色。
    • 谢谢。尝试 [button setTitleColor:fooColor forState:UIControlStateNormal] 代替。
    • 人们,停止遍历 Apple 的私有控件子视图。这很糟糕,总有一天会崩溃,并且会让其他开发人员嘲笑你。
    • 您使用的是您认为 Apple 不会改变的东西,但在 iOS 8 中并非如此,代码不安全
    • 伙计们,这不是使用私有 API; -[UIView subviews] 非常公开。但是以这种方式改变文本颜色当然不是苹果的本意,所以你可以说它是“在精神上”的私人。如果您的设计师或产品负责人要求这样做,这里有一种方法。如果您认为不应该这样做,请不要这样做。故事结束。
    【解决方案2】:

    在 iOS 8 中,这些都不再适用了。 UIActionSheet 文本似乎从 window.tintColor 获取颜色。

    【讨论】:

    • 抱歉回复晚了。我的解决方法是停止将 window.tintColor 或 self.view.tintColor 更改为白色。相反,我现在明确设置任何需要白色 tintColor 的元素,而不是依赖父视图 tintColors。
    • 好帖子!我一直想知道为什么我的UIAlertViewsUIAlertController 出现在whiteColor 中。
    • 在 iOS8 中,不推荐使用 UIActionSheet。您可以使用 UIAlertController 并在警报控制器视图上设置 tintColor。
    【解决方案3】:

    这适用于我在 iOS8 中

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

    【讨论】:

    • 这将改变所有按钮的颜色,包括取消按钮。有没有办法将颜色更改为红色,但不能用于取消按钮?
    【解决方案4】:

    如果你想通过UIActionSheet的子视图,你不应该直接设置UILabel的文本颜色,而是使用UIButtonsetTitleColor:forState方法。否则,初始颜色将在诸如 UIControlEventTouchDragOutside 之类的事件时重新设置。

    这是正确的方法,重用 jrc 的代码:

    - (void)willPresentActionSheet:(UIActionSheet *)actionSheet
    {
        for (UIView *subview in actionSheet.subviews) {
            if ([subview isKindOfClass:[UIButton class]]) {
                UIButton *button = (UIButton *)subview;
                [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            }
        }
    }
    

    【讨论】:

      【解决方案5】:

      可能是一个迟到的答案,但我认为它可以帮助某人。

      iOS 8: 您可以简单地使用以下样式初始化 UIAlertAction:UIAlertActionStyleDestructive,如下所示:

      UIAlertAction *delete = [UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action){
      
         // your code in here   
      
      }];
      

      这将使按钮的文本默认为红色。

      【讨论】:

        【解决方案6】:

        要在警报操作中更改特定按钮的颜色,请使用此

        let cancelAction = UIAlertAction(title: "Success", style: .cancel, handler: nil)
        cancelAction.setValue(UIColor.green, forKey: "titleTextColor")
        
        

        【讨论】:

          【解决方案7】:

          @jrc 解决方案有效,但当您点击按钮时,titleLabel 颜色会发生变化。试试这个以在所有状态下保持相同的颜色。

          - (void)willPresentActionSheet:(UIActionSheet *)actionSheet
          {     
           UIColor *customTitleColor = [UIColor greenColor];
                for (UIView *subview in actionSheet.subviews) {
                  if ([subview isKindOfClass:[UIButton class]]) {
                    UIButton *button = (UIButton *)subview;
          
                    [button setTitleColor:customTitleColor forState:UIControlStateHighlighted];
                    [button setTitleColor:customTitleColor forState:UIControlStateNormal];
                    [button setTitleColor:customTitleColor forState:UIControlStateSelected];
                  }
                }
          }
          

          【讨论】:

            【解决方案8】:

            Swift 3 的解决方案。更改所有颜色。

            UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = .yellow
            

            【讨论】:

            • 这将改变您应用中使用的所有 UIAlertController 的颜色。
            • 但我这边 +1。
            【解决方案9】:

            编辑

            这里有一个类似的问题:How to customize Buttons in UIActionSheet?

            尝试使用appearance 协议[不工作]

             UIButton *btn = [UIButton appearanceWhenContainedIn:[UIActionSheet class], nil];
             [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            

            【讨论】:

              【解决方案10】:

              将此用于 ios 8 及更高版本

              SEL selector = NSSelectorFromString(@"_alertController");
              if ([actionSheet respondsToSelector:selector])
              {
                  UIAlertController *alertController = [actionSheet valueForKey:@"_alertController"];
                  if ([alertController isKindOfClass:[UIAlertController class]])
                  {
                      alertController.view.tintColor = [UIColor blueColor];
                  }
              }
              else
              {
                  // use other methods for iOS 7 or older.
              }
              

              【讨论】:

                【解决方案11】:

                UIAppearance 可以处理这个:

                [[UIButton appearanceWhenContainedIn:[UIActionSheet class], nil] setTitleColor:[UIColor cyanColor] forState:UIControlStateNormal];
                

                比浏览子视图容易得多,但我还没有在 iOS 6 或更早版本上测试过这个解决方案。

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2017-04-10
                  • 2011-03-14
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多