2010-11-22 99 views

回答

34

的iOS 8(UIAlertController)

这是超级简单的做,如果您使用的是UIAlertController。只需更改UIAlertController视图上的色调颜色即可。

[alertController.view setTintColor:[UIColor red]; 

的iOS 7(UIActionSheet)

我通过使用这种简单的方法成功改变文本的颜色。

- (void) changeTextColorForUIActionSheet:(UIActionSheet*)actionSheet { 
    UIColor *tintColor = [UIColor redColor]; 

    NSArray *actionSheetButtons = actionSheet.subviews; 
    for (int i = 0; [actionSheetButtons count] > i; i++) { 
     UIView *view = (UIView*)[actionSheetButtons objectAtIndex:i]; 
     if([view isKindOfClass:[UIButton class]]){ 
      UIButton *btn = (UIButton*)view; 
      [btn setTitleColor:tintColor forState:UIControlStateNormal]; 

     } 
    } 
} 

确保运行此你叫

[actionSheet showInView]; 

如果你之前[showInView]调用它,所有的按钮,但取消按钮将被着色。希望这可以帮助别人!

1

不幸的是,没有使用未记录的API,没有官方的方法来改变UIActionSheet上的按钮颜色。如果您继承UIActionSheet控件,您可能可以自定义这个。

见这个例子:http://blog.corywiles.com/customizing-uiactionsheet-buttons

+0

你有没有这样做实现呢?任何示例代码都会非常有帮助。 – Abhinav 2010-11-22 17:47:08

+0

查看我上面发布的链接。 – 2010-11-22 17:50:23

+0

非常感谢你! – Abhinav 2010-11-22 18:21:18

1

我们可以使用背景图像来做到这一点。我认为这是最简单的方法。

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Actionsheet" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]; 
     [actionSheet addButtonWithTitle:@"Button 1"]; //Blue color 
     [actionSheet addButtonWithTitle:@"Button 2"]; 
     [actionSheet addButtonWithTitle:@"Cancel"]; 
     [actionSheet addButtonWithTitle:nil]; 
     [actionSheet setCancelButtonIndex:2]; 
     [actionSheet setDestructiveButtonIndex:1]; 
     [actionSheet showInView:self.view]; 
     UIButton *button = [[actionSheet subviews] objectAtIndex:1]; 
     UIImage *img = [button backgroundImageForState:UIControlStateHighlighted];//[UIImage imageNamed:@"alert_button.png"]; 
     [button setBackgroundImage:img forState:UIControlStateNormal]; 
0

您可以轻松地通过使用下面的代码

苹果UIActionSheetDelegate协议文档

https://developer.apple.com/library/ios/documentation/uikit/reference/UIModalViewDelegate_Protocol/UIActionSheetDelegate/UIActionSheetDelegate.html

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet 
{ 
    for (UIView *_currentView in actionSheet.subviews) 
    { 
     if ([_currentView isKindOfClass:[UIButton class]]) 
     {  
      UIButton *button = (UIButton *)_currentView; 
      [button setTitleColor:YOUR_COLOR forState:UIControlStateNormal]; 
     } 
    } 
}