UIActionSheet按钮的颜色

16

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

5个回答

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
+1 是针对这句话“确保在调用之后运行此命令”的。 - arunit21
有没有想法为什么只有在竖屏模式下按钮显示为红色,但在横屏模式下只有取消按钮被着色。 - Fouad

7
我创建了一个名为UICustomActionSheet的子类,可以自定义UIActionSheet中按钮的字体、颜色和图片。这对于App Store是绝对安全的,你可以在下面的链接中找到这个类的代码:

https://github.com/gloomcore/UICustomActionSheet

享受它!


你确定这对于App Store来说没问题吗?不管怎样,结果很棒! - Emanuele Fumagalli
如果我在运行时使用了许多其他按钮并设置了其字体,取消按钮将无法正常工作。 - 9to5ios
现在它不兼容iOS 7。 - Irfan DANISH
抱歉让您等待这么久,我已经推送了最新更改以实现与iOs7和扁平化设计的兼容性。所以您可以再次使用它。 - Gloomcore
此消息出现在7.0版本中。UiActionSheet不是为了被子类化而设计的。但这个更改并不是关键性的,也没有被苹果团队拒绝,因为它没有使用私有API,也没有受到任何其他限制的覆盖。 - Gloomcore
显示剩余2条评论

1

你曾经做过这个吗?任何示例代码都将非常有帮助。 - Abhinav
3
改变按钮颜色并不困难,而且您可以在不使用未公开的API的情况下完成它。 - Rymnel
不应该对 UIActionSheet 进行子类化。根据文档:UIActionSheet 不是为了被子类化设计的,也不应该向其层次结构中添加视图。如果您需要呈现一个比 UIActionSheet API 提供更多自定义的 sheet,您可以创建自己的 sheet 并使用 presentViewController:animated:completion: 模态呈现它。 - memmons

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接