UIAlertController更改操作表的“取消”button的背景颜色
我想创build一个UIAlertController的动作表风格,但我想要将背景颜色更改为灰色。 我已经能够find一种方法来更改UIAlertController的背景颜色,而不是取消button。 取消button,单独,保持白色。
这是我现在的代码:
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];
这给了我以下结果: 链接
我曾经访问过如何更改UIAlertController的背景颜色? 但没有一个解决scheme具有自定义颜色的取消button的背景。
任何帮助,将不胜感激!
如果你想单独的取消button(UIAlertActionStyleCancel),你不能改变取消button的背景颜色。 如果这是你的优先,那么你必须做出自己的自定义视图。 否则,您可以简单地添加标题为“取消”的默认操作。
[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:nil]]
(但它不会给你一个单独的button)。
我已经debugging了视图层次结构,并发现这一点。
我现在得到了解决scheme。 我创build了示例项目,并解决了你的问题,我明白了。
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
看到输出
- iOS 9 … WebView是免除应用程序传输安全例外(ATS)规则,阻止不安全的HTTP主机?
- iPhone 6和iPhone 6将如何为iPhone 4和iPhone 5构build应用程序
- 什么是void *以及它可以指向哪些variables/对象
- 模糊使用select器dataTask(with:completionHandler :)
- 如何将部署目标更改为XCode5中的5.1.1
- IOS – 暂停和取消暂停播放音乐
- 存储在上下文中的对象的潜在泄漏
- 当在两个嵌套子类中调用super.viewDidLoad()时,我得到了Swift无限循环
- 单个UIViewcontroller,如何在多个xib之间切换?