2017-06-17 315 views
0

我想創建一個具有操作表樣式的UIAlertController,但我想將背景顏色更改爲灰色。我已經能夠找到一種方法來改變UIAlertController的背景顏色,但不是取消按鈕。取消按鈕是單獨的,保持白色。UIAlertController更改操作表的取消按鈕的背景顏色

這是我現在所擁有的代碼:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; 

[alert addAction:[UIAlertAction actionWithTitle:@"Option 1" style:UIAlertActionStyleDefault handler:nil]]; 
[alert addAction:[UIAlertAction actionWithTitle:@"Option 2" style:UIAlertActionStyleDefault handler:nil]]; 
[alert addAction:[UIAlertAction actionWithTitle:@"Option 3" style:UIAlertActionStyleDefault handler:nil]]; 
[alert addAction:[UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDestructive handler:nil]]; 

[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]]; 

UIView *firstSubview = alert.view.subviews.firstObject; 
UIView *alertContentView = firstSubview.subviews.firstObject; 

for (UIView *subSubView in alertContentView.subviews) { 
    subSubView.backgroundColor = [UIColor darkGrayColor]; // Here you change background 
} 

alert.view.tintColor = [UIColor whiteColor]; 

[self.controller presentViewController:alert animated:YES completion:nil]; 

這使我有以下結果: Link

我曾參觀過How to change the background color of the UIAlertController? 但沒有任何解決方案都爲自定義顏色取消按鈕的背景。

任何幫助,將不勝感激!

+0

是否要更改取消按鈕的背景顏色? – user3182143

+0

查看我的編碼我將取消按鈕顏色更改爲darkGray顏色。 – user3182143

+0

我希望取消按鈕具有深灰色背景色和白色文本。你的代碼似乎沒有這樣做,但是相反:你的取消按鈕的背景顏色是白色的,而你的文本顏色是灰色的 – Pibomb

回答

0

我現在得到了解決方案。我創建了示例項目,併爲您的問題制定瞭解決方案,並且獲得了它。

ViewController.h

#import <UIKit/UIKit.h> 
@interface ViewController : UIViewController 
@end 

ViewController.m

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; 

    [alert addAction:[UIAlertAction actionWithTitle:@"Option 1" style:UIAlertActionStyleDefault handler:nil]]; 
    [alert addAction:[UIAlertAction actionWithTitle:@"Option 2" style:UIAlertActionStyleDefault handler:nil]]; 
    [alert addAction:[UIAlertAction actionWithTitle:@"Option 3" style:UIAlertActionStyleDefault handler:nil]]; 
    [alert addAction:[UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDestructive handler:nil]]; 

    [alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]]; 

    UIView *subView = alert.view.subviews.lastObject; //firstObject 
    UIView *alertContentView = subView.subviews.lastObject; //firstObject 
    [alertContentView setBackgroundColor:[UIColor darkGrayColor]]; 
    alertContentView.layer.cornerRadius = 5; 
    [self presentViewController:alert animated:YES completion:nil]; 
    alert.view.tintColor = [UIColor darkGrayColor]; 
} 


- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


@end 

看到輸出

enter image description here

+1

我認爲你沒有正確理解我的問題。取消按鈕的背景顏色仍然是白色,並且您的文本現在變成灰色。我需要相反的事情發生 – Pibomb

4

如果你想要一個單獨取消按鈕(UIAlertActionStyleCancel)你不能更改取消按鈕的背景顏色。如果這是你的優先,那麼你必須做出自己的自定義視圖。否則,您可以簡單地添加標題爲「取消」的默認操作。

[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:nil]]

(但它不會給你一個單獨的按鈕)。

我調試了視圖層次結構,發現這一點。 enter image description here

0

有一個非常非常骯髒的解決方案,但它的工作原理。爲此,我們將使用UIAppearance

首先,我們需要爲UIView準備一個特殊的專用擴展,以便能夠更改它的子視圖背景顏色。

fileprivate extension UIView { 
    private struct AssociatedKey { 
     static var subviewsBackgroundColor = "subviewsBackgroundColor" 
    } 

    @objc dynamic var subviewsBackgroundColor: UIColor? { 
     get { 
      return objc_getAssociatedObject(self, &AssociatedKey.subviewsBackgroundColor) as? UIColor 
     } 

     set { 
      objc_setAssociatedObject(self, 
            &AssociatedKey.subviewsBackgroundColor, 
            newValue, 
            .OBJC_ASSOCIATION_RETAIN_NONATOMIC) 
      subviews.forEach { $0.backgroundColor = newValue } 
     } 
    } 
} 

我們每次設定subiewsBackgroundColor值的UIView會遍歷它的子視圖,併爲每一個新的背景顏色。

正如你可以在答案見下面有一個叫_UIAlertControlleriOSActionSheetCancelBackgroundView在UIAlertController的層次結構特殊的UIView其中包含與白色顏色的子視圖(Cancel按鈕)

讓我們試着去得到它的外觀和使用我們的屬性來改變它的子視圖顏色。我想這是一種私人API。

if let cancelBackgroundViewType = NSClassFromString("_UIAlertControlleriOSActionSheetCancelBackgroundView") as? UIView.Type { 
    cancelBackgroundViewType.appearance().subviewsBackgroundColor = .red 
} 

將此代碼放在AppDelegate的didFinishLaunching或專用類中。 就是這樣。現在您可以看到紅色背景上的取消按鈕。在iOS 11上測試。