UIAlertController 背景颜色 iOS10
UIAlertController background color iOS10
我为 iOS9 编写的代码运行得非常好:
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Select source"
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
alert.view.backgroundColor = DARK_BLUE;
alert.view.tintColor = NEON_GREEN;
UIView *subview = alert.view.subviews.firstObject;
UIView *alertContentView = subview.subviews.firstObject;
alertContentView.backgroundColor = DARK_BLUE;
alertContentView.layer.cornerRadius = 10;
我的观点是UIAlertController
是继承自UIViewController
,而UIViewController
有属性UIView
可以改变。这是有效的。现在,从 UIViewController
继承的那个视图有它自己的子视图,即显示为警报的 contentView。我可以在子视图数组中将其作为 firstObject 访问。现在,为什么发送背景颜色的消息不再起作用了?有人知道一些新的解决方案吗?
我用这个:
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Alert!"
message:@"Message of alert"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"Ok"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//Your code
}];
[alert addAction:defaultAction];
UIView * topview = alert.view.subviews.firstObject;
UIView * colorView = topview.subviews.firstObject;
colorView.backgroundColor = [UIColor yellowColor];
colorView.layer.cornerRadius = 10;
[self presentViewController:alert animated:YES completion:nil];
对于遇到同样问题的每个人,我找到了解决方案:
UIAlertController.view
包含一个子视图,即只有 container.
该子视图包含子视图,其中包含两个它自己的子视图,一个是容器,另一个是模糊它的层。
因此,它需要在循环中遍历这两个子视图并更改两者的背景颜色。
完整代码:
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Select source"
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
alert.view.tintColor = NEON_GREEN;
UIView *firstSubview = alert.view.subviews.firstObject;
UIView *alertContentView = firstSubview.subviews.firstObject;
for (UIView *subSubView in alertContentView.subviews) { //This is main catch
subSubView.backgroundColor = DARK_BLUE; //Here you change background
}
在Swift3.0
let FirstSubview = alertController.view.subviews.first
let AlertContentView = FirstSubview?.subviews.first
for subview in (AlertContentView?.subviews)! {
subview.backgroundColor = UIColor.black
subview.layer.cornerRadius = 10
subview.alpha = 1
subview.layer.borderWidth = 1
subview.layer.borderColor = UIColor.yellow.cgColor
}
我为 iOS9 编写的代码运行得非常好:
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Select source"
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
alert.view.backgroundColor = DARK_BLUE;
alert.view.tintColor = NEON_GREEN;
UIView *subview = alert.view.subviews.firstObject;
UIView *alertContentView = subview.subviews.firstObject;
alertContentView.backgroundColor = DARK_BLUE;
alertContentView.layer.cornerRadius = 10;
我的观点是UIAlertController
是继承自UIViewController
,而UIViewController
有属性UIView
可以改变。这是有效的。现在,从 UIViewController
继承的那个视图有它自己的子视图,即显示为警报的 contentView。我可以在子视图数组中将其作为 firstObject 访问。现在,为什么发送背景颜色的消息不再起作用了?有人知道一些新的解决方案吗?
我用这个:
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Alert!"
message:@"Message of alert"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"Ok"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//Your code
}];
[alert addAction:defaultAction];
UIView * topview = alert.view.subviews.firstObject;
UIView * colorView = topview.subviews.firstObject;
colorView.backgroundColor = [UIColor yellowColor];
colorView.layer.cornerRadius = 10;
[self presentViewController:alert animated:YES completion:nil];
对于遇到同样问题的每个人,我找到了解决方案:
UIAlertController.view
包含一个子视图,即只有 container.
该子视图包含子视图,其中包含两个它自己的子视图,一个是容器,另一个是模糊它的层。
因此,它需要在循环中遍历这两个子视图并更改两者的背景颜色。
完整代码:
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Select source"
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
alert.view.tintColor = NEON_GREEN;
UIView *firstSubview = alert.view.subviews.firstObject;
UIView *alertContentView = firstSubview.subviews.firstObject;
for (UIView *subSubView in alertContentView.subviews) { //This is main catch
subSubView.backgroundColor = DARK_BLUE; //Here you change background
}
在Swift3.0
let FirstSubview = alertController.view.subviews.first
let AlertContentView = FirstSubview?.subviews.first
for subview in (AlertContentView?.subviews)! {
subview.backgroundColor = UIColor.black
subview.layer.cornerRadius = 10
subview.alpha = 1
subview.layer.borderWidth = 1
subview.layer.borderColor = UIColor.yellow.cgColor
}