UIActionSheet按钮的颜色

时间:2010-11-22 17:40:08

标签: iphone cocoa-touch uikit

如何更改UIActionSheet按钮颜色的颜色?

5 个答案:

答案 0 :(得分: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];

        }
    }
}

请务必先运行 AFTER

[actionSheet showInView];

如果您在[showInView]之前调用它,除了取消按钮之外的所有按钮都将被着色。希望这有助于某人!

答案 1 :(得分:7)

我创建了子类UICustomActionSheet,它允许在UIActionSheet中自定义按钮的字体,颜色和图像。对于appstore来说绝对安全,你可以在下一个链接上找到这个类的代码:

https://github.com/gloomcore/UICustomActionSheet

享受它!

答案 2 :(得分:1)

不幸的是,如果不使用未记录的API,则无法在UIActionSheet上更改按钮颜色的官方方法。如果您继承UIActionSheet控件,则可以自定义此项。

请参阅此示例:http://blog.corywiles.com/customizing-uiactionsheet-buttons

答案 3 :(得分: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];

答案 4 :(得分:0)

您可以使用以下代码轻松实现

  

Apple 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];
        }
    }
}
相关问题