Pregunta

I've created UIAlertView with multiple buttons with the following code:

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];

I'm wondering if it is possible to add a different background color to each button?

¿Fue útil?

Solución

Yes, you can but should not modify the look of UIAlertView since Apple hates that shit.
Instead, create your own UIView that mimics the alertView.

Also... does the initWithFrame method even make a difference?
Example... Try:

UIAlertView *alert = [[UIAlertView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];

It won't make a difference.

So... I'd suggest:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notes"
                                                message:nil
                                               delegate:self
                                      cancelButtonTitle:@"Cancel"
                                      otherButtonTitles:@"Add Note",@"Show Notes",nil];
[alert setTag:TAG_ALERT0];
[alert show];

But since you asked for sample code, the following example will be simple and to the point:

STATUTORY WARNING:

  1. Apple doesn't like this
  2. Doesn't work in iOS7

Example:

-(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]];
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top