2015-09-21 145 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 
} 
相關問題