2015-09-21 144 views
29

我使用以下代码将项目文本显示为红色的UIAlertController操作表。我使用了tint属性来设置颜色。UIAlertController着色颜色默认为高亮显示为蓝色

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

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

+0

要改变文字颜色为红色或背景色为红色在所有模式 –

+0

它有点不清楚你想要达到的目标,但这里看看,我想你会在这个线程中找到答案http://stackoverflow.com/questions/4248400/uiactionsheet-buttons-color – deimus

+0

@deimus我已经改变了文字颜色为红色的行动表项目。哪些工作正常,但是当您突出显示或选择操作表项时。它默认为蓝色。我希望它保持红色。 –

回答

0

我仍然困惑你想达到什么。但是,您可以尝试使用Apple创建Destructive按钮的方式(默认文本颜色为红色)。

您正在创建的代码UIAlertAction s不使用Default样式作为您想要的红色按钮。请使用UIAlertActionStyleDestructive。示例代码:

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

         }]; 
45

这是一个已知的bug,请参阅https://openradar.appspot.com/22209332

为了解决这个问题,重新在完成处理器色调颜色。这里是我的迅速解决,你就能够轻松适应它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() 
}) 

一注:这不完全修复的Bug。设备旋转时,您会注意到按钮以系统默认(=蓝色)色调重新着色。

预计它将在iOS 9.1中修复。

编辑10/23/2015:仍然没有固定与iOS 9.1。几天前发布的iOS 9.1 + Xcode 7.1(7B91B)重新测试。截至目前设置的.tintColor不起作用,但作为评论,你可以设置整个应用程序的tintColor,例如在AppDelegate didFinishLaunchingWithOptions中设置了window?.tintColor = UIColor.redColor()这也会提示AlertController按钮,但可能不适用于某些情况,因为此色调适用于整个应用程序。

+0

谢谢。我将在iOS 9.1发布时再次检查。 –

+0

不客气:) –

+1

附录:仍未修复iOS 9.0.1和9.0.2。等待iOS 9.1。作为临时解决方法,可能会发现适用于在整个应用程序中应用AppDelegate didFinishLaunchingWithOptions中的整个应用程序的tintColor'window?.tintColor = UIColor.redColor()'。但是这可能会干扰您的其他用户界面方面。 –

36

只需在presentViewController后添加tintColor即可。在iOS 9.0.2上工作

[self presentViewController:alertController animated:YES completion:nil]; 

[alertController.view setTintColor:[UIColor yellowColor]]; 
+0

使用@Eddy方法。完美的作品。谢谢! –

+0

刚刚测试过:旋转设备时不起作用。接受这只是错误的。 AlertController在旋转时将Tint更改回蓝色。所以这不是正确答案;)这段代码片段与我的Completion Handler解决方法一样适用于Portrait,但请不要认为此解决方案存在一些Code Smell,因为调用/呈现alertController的消息可能会提前返回并设置它以这种方式查看色调 - 不在完成处理程序中 - 可能会导致不需要的行为。 –

+0

@ frederik-a-winkelsdorf我是新人,并不是所有的规则。我接受了答案,因为它解决了我的问题。颜色默认为突出或选择。我现在还没有接受答案。 –

0

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

0

为了防止快速“弹出”新的色调颜色,警报控制器的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) 
    } 
11

您还可以更改appdelegate中的应用程序着色颜色。

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

对我来说很完美。

+1

令人震惊的是,这个工作。即使在故事板中设置全局色调颜色,它也不适用于任何AlertController,并且如果直接将色调颜色应用于警报,则按钮的“突出显示”状态仍为默认蓝色。但是,添加上面的代码实际上正确地将您的色彩应用于警报。 –

1

将您的色调颜色设置为traitCollectionDidChange的子类UIAlertController

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

要设置自定义颜色字体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) 
4

您可以更改按钮的颜色像这样

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

通过使用此行[按钮的setValue: UIColor redColor] forKey:@“titleTextColor”]; 你可以改变你的动作表的按钮颜色

+2

有用的答案... –

+0

这个解决方案工作 – christijk

+0

是@ Anbu.Karthik它的工作完美...! –

1

只是改变你的看法tintAdjustmentMode UIViewTintAdjustmentModeNormal,所以它不会改变它在dimm上的颜色。

0

更新斯威夫特4时,Xcode 9

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