[英]How to change a UIAlertView's button background color?
我用以下代码创建了带有多个按钮的UIAlertView
:
UIAlertView *alert = [[UIAlertView alloc] initWithFrame:CGRectMake(0, 0, 300, 200)];
alert.tag = TAG_ALERT0;
alert.title = @"Notes";
alert.delegate = self;
[alert addButtonWithTitle:@"Add Note"];
[alert addButtonWithTitle:@"Show Notes"];
[alert addButtonWithTitle:@"Cancel"];
[alert show];
我想知道是否可以为每个按钮添加不同的背景颜色?
是的,您可以但不应修改UIAlertView
的外观,因为Apple讨厌这样。
而是创建您自己的模仿alertView
的UIView
。
另外... initWithFrame
方法是否有所作为?
示例...尝试:
UIAlertView *alert = [[UIAlertView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
不会有什么不同。
所以...我建议:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notes"
message:nil
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Add Note",@"Show Notes",nil];
[alert setTag:TAG_ALERT0];
[alert show];
但是,由于您要求提供示例代码,因此以下示例很简单并且很重要:
例:
-(void)willPresentAlertView:(UIAlertView *)alertView
{
UILabel *theTitle = [alertView valueForKey:@"_titleLabel"];
[theTitle setTextColor:[UIColor orangeColor]];
NSMutableArray *arrButtons = [alertView valueForKey:@"_buttons"];
for (UIView *vwCurrent in arrButtons) {
[vwCurrent setBackgroundColor:[UIColor redColor]];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.