【问题标题】:UIAlertController tint color defaults to blue on highlightUIAlertController tint color 在高亮时默认为蓝色
【发布时间】:2015-12-18 01:03:10
【问题描述】:

我正在使用以下代码来呈现一个 UIAlertController 操作表,其中项目文本为红色。我使用了 tint 属性来设置颜色。

UIAlertController *alertController = [UIAlertController
                                      alertControllerWithTitle:nil
                                      message:nil
                                      preferredStyle:UIAlertControllerStyleActionSheet];
alertController.view.tintColor = [UIColor redColor];

文本颜色似乎在突出显示或选择时默认为蓝色。这是正常的吗?我该如何阻止它?

【问题讨论】:

  • 您想在所有模式下将文本颜色更改为红色或将背景颜色更改为红色
  • 有点不清楚你想要实现什么,但看看这里,我想你会在这个线程中找到答案stackoverflow.com/questions/4248400/uiactionsheet-buttons-color
  • @deimus 我已将操作表项目的文本颜色更改为红色。哪个可以正常工作,但是当您突出显示或选择操作表项时。它默认为蓝色。我希望它保持红色。
  • @BcfAnt 如果我的信息对您来说足够了,您介意接受答案吗? Apple Bug 不是我的错 ;)
  • Frederick- 如果 Apple 的编码方式意味着它不起作用,那么他接受您的解决方案是不正确的。当有人提出不适用于轮换的解决方案时,您在下面的回复中说了类似的话。

标签: ios objective-c ios9 uialertcontroller


【解决方案1】:

Swift 4、Xcode 9 更新

private static func setupAppearanceForAlertController() {
    let view = UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self])
    view.tintColor = .black
}

【讨论】:

  • 工作完美,谢谢!
【解决方案2】:

只需将您的视图 tintAdjustmentMode 更改为 UIViewTintAdjustmentModeNormal,这样它就不会在暗淡时改变它的颜色。

【讨论】:

    【解决方案3】:

    这是一个已知的Bug,见https://openradar.appspot.com/22209332

    要修复它,请在 Completion 处理程序中重新应用色调颜色。这是我的 Swift 解决方案,您将能够轻松地将其适应 ObjC:

    alertController.view.tintColor = UIColor.redColor() // apply 1st time to prevent flicker from Blue to Red when displaying
    
    navigationController?.presentViewController(alertController, animated: true, completion: {
        // Bugfix: iOS9 - Tint not fully Applied without Reapplying
        alertController.view.tintColor = UIColor.redColor()
    })
    

    注意:这并不能完全修复错误。您会在设备旋转时注意到按钮已使用系统默认(= 蓝色)色调重新着色。

    预计它会在 iOS 9.1 中修复。

    2015 年 10 月 23 日编辑:iOS 9.1 仍未修复。使用几天前发布的 iOS 9.1 + Xcode 7.1 (7B91B) 重新测试。截至目前,设置 .tintColor 不起作用,但如评论所述,您可以设置整个应用程序的 tintColor,例如在 AppDelegate didFinishLaunchingWithOptions 设置window?.tintColor = UIColor.redColor()这也会为 AlertController 按钮着色,但在某些情况下可能不适合,因为此着色会应用于整个应用程序。

    【讨论】:

    • 谢谢。我会在 iOS 9.1 发布时再次检查。
    • 附录:iOS 9.0.1 和 9.0.2 仍未修复。等待 iOS 9.1。作为一种临时解决方法,人们可能会发现在 AppDelegate didFinishLaunchingWithOptions window?.tintColor = UIColor.redColor() 中设置整个应用程序的 tintColor 是合适的,因为这适用于整个应用程序。但这可能会干扰您的其他 UI 方面。
    • 仍然没有修复,但是如果在呈现视图后设置色调颜色,它不会默认恢复为原始颜色。请参阅下面 Eddy 的回答。
    • @Bcf Ant:也在轮换? Afaik 这也不能解决问题,我稍后再检查..
    • @frederik-a-winkelsdorf 我只测试了一个锁定为纵向的应用程序。如果您的应用也支持横向,则问题可能仍然存在。
    【解决方案4】:

    你可以像这样改变按钮颜色

    UIAlertAction* button = [UIAlertAction
                                  actionWithTitle:@"Delete Profile"
                                  style:UIAlertActionStyleDefault
                                  handler:^(UIAlertAction * action)
                                  {
                                      //Add Action.
                                  }];
    [button setValue:[UIColor redColor] forKey:@"titleTextColor"];
    

    通过使用这一行 [button setValue:[UIColor redColor] forKey:@"titleTextColor"]; 您可以更改操作表的按钮颜色

    【讨论】:

      【解决方案5】:

      像这样设置自定义colorfont子类UIAlertController

      import UIKit
      
      class StyledAlertController: UIAlertController {
      
          private var cancelText:String?
      
          override func viewDidLoad() {
              super.viewDidLoad()
              view.tintColor = YourColor
          }
      
          override func viewWillLayoutSubviews() {
              super.viewWillLayoutSubviews()
              findLabel(view)
          }
      
          private func findLabel(scanView: UIView!) {
              if (scanView.subviews.count > 0) {
                  for subview in scanView.subviews {
                      if let label:UILabel = subview as? UILabel {
                          if (cancelText != nil && label.text == cancelText!) {
                              dispatch_async(dispatch_get_main_queue(),{
                                  label.textColor = YourColor
                                  label.tintColor = YourColor
                              })
                          }
                          let font:UIFont = UIFont(name: YourFont, size: label.font!.pointSize)!
                          label.font = font
                      }
                      findLabel(subview)
                  }
              }
          }
      }
      

      像这样使用(像往常一样)

      let StyledAlertController = StyledAlertController(title: "My Title", message: "My Message", preferredStyle: .ActionSheet)
      
      let cancelAction:UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
              print("Cancel Action Click")
          }
      actionSheetController.addAction(cancelAction)
      
      let anotherAction:UIAlertAction = UIAlertAction(title: "Another Action", style: .Default) { action -> Void in
              print("Another Action Click")
          }
      actionSheetController.addAction(anotherAction:UIAlertAction)
      
      presentViewController(actionSheetController, animated: true, completion: nil)
      

      【讨论】:

        【解决方案6】:

        traitCollectionDidChange 的子类UIAlertController 中设置您的色调颜色。

        override func traitCollectionDidChange(previousTraitCollection: UITraitCollection?) {
            super.traitCollectionDidChange(previousTraitCollection)
            self.view.tintColor = UIColor.redColor()
        }
        

        【讨论】:

          【解决方案7】:

          您还可以在 appdelegate 中更改应用的色调。

          - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
          {
              self.window.tintcolor = [UIColor yellowColor];
              return YES;
          }
          

          非常适合我。

          【讨论】:

          • 令人震惊的是,这行得通。即使您在情节提要中设置了全局色调颜色,它也不会应用于任何 AlertControllers,如果您直接将色调颜色应用于警报,按钮的“突出显示”状态仍然是默认蓝色。但是,添加上面的代码实际上会正确地将您的色调应用于警报。
          【解决方案8】:

          为了防止新的色调快速“弹出”,警报控制器的 alpha 值可以设置动画。然后它看起来就像没有错误一样:

              alertController.view.alpha = 0.0
              presentViewController(alertController, animated: false) {
                  alertController.view.tintColor = UIColor.redColor()
                  UIView.animateWithDuration(0.2, animations: { alertController.view.alpha = 1.0 }, completion: nil)
              }
          

          【讨论】:

            【解决方案9】:

            UIAlertController 适用于 iOS 8 及更高版本,因此对于旧版本的设备存在错误。对应或更高版本的设备没有问题。

            【讨论】:

              【解决方案10】:

              只需在 presentViewController 之后添加 tintColor。适用于 iOS 9.0.2

              [self presentViewController:alertController animated:YES completion:nil];
              
              [alertController.view setTintColor:[UIColor yellowColor]];
              

              【讨论】:

              • 使用@Eddy 方法。完美运行。谢谢!
              • 刚测试:旋转设备时不起作用。接受这一点是错误的。 AlertController 在旋转时将 Tint 更改回 Blue。所以这不是正确的答案;)此代码 sn-p 适用于 Portrait,与我的 Completion Handler Workaround 一样,但请不要认为此解决方案有一些代码气味,因为调用/呈现 alertController 的消息可能会提前返回并以这种方式设置它的 View Tint - 而不是在 Completion Handler 中 - 可能会导致不需要的行为。
              • @frederik-a-winkelsdorf 我是新手,不遵守所有规则。我接受了答案,因为它解决了我的问题。颜色默认为突出显示或选择。我暂时不接受答案。
              • 这里有一个很好的替代UIAlertControllergithub.com/AaoIi/AAAlertController
              【解决方案11】:

              我仍然对您想要实现的目标感到困惑。但是您可以尝试 Apple 创建Destructive 按钮的方式(默认文本颜色为红色)。

              您在其中创建UIAlertActions 的代码不要将Default 样式用于您想要的红色按钮。而是使用UIAlertActionStyleDestructive。示例代码:

              UIAlertAction* cancel = [UIAlertAction
                                       actionWithTitle:@"Cancel"
                                       style:UIAlertActionStyleDestructive
                                       handler:^(UIAlertAction * action)
                                       {
                                           [view dismissViewControllerAnimated:YES completion:nil];
              
                                       }];
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2019-11-10
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2021-03-21
                相关资源
                最近更新 更多