设置UIAlert按钮颜色

需求:将UIAlertView的button文字改成指定颜色:

代码如下:

1
2
3
4
5
6
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:@"设置按钮为红色" delegate:nil cancelButtonTitle:@"我是红色" otherButtonTitles:nil];

[[UIView appearance] setTintColor:[UIColor redColor]];

[alertView show];      

然后要在点击按钮的时候将tintColor置空,因为默认UIView appearance的tintColor是为nil的:

1
2
3
4
5
6
+ (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
  if (buttonIndex == [alertView cancelButtonIndex]) {
      [[UIView appearance] setTintColor:nil];
  }
}
-

Copyright © 2015 - 徐佳琦 - Powered by Octopress