[英]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.