【问题标题】:UIActionSheet button's colorUIActionSheet 按钮的颜色
【发布时间】:2010-11-22 17:40:07
【问题描述】:

如何更改 UIActionSheet 按钮的颜色?

【问题讨论】:

    标签: iphone cocoa-touch uikit


    【解决方案1】:

    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] 之前调用它,则除取消按钮之外的所有按钮都将着色。希望这对某人有帮助!

    【讨论】:

    • @dclowd9901 谢谢! ;)
    • +1 表示“请确保在您致电后运行此程序”以获得此报价。
    • 知道为什么只有纵向模式下的按钮显示为红色,而横向模式下只有取消按钮被着色。
    【解决方案2】:

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

    https://github.com/gloomcore/UICustomActionSheet

    尽情享受吧!

    【讨论】:

    • 您确定 App Store 可以吗?反正结果很好!
    • 是的,我确定,我已经在 Appstore 的应用程序中使用过它。这个类没有使用私有方法,所以很清楚。
    • 太棒了!感谢发帖!
    • 如果我使用了许多其他按钮并设置其字体运行时,取消按钮将不起作用
    • 它现在不兼容 iOS 7。
    【解决方案3】:

    很遗憾,如果不使用未记录的 API,则没有官方方法可以更改 UIActionSheet 上按钮的颜色。如果您将 UIActionSheet 控件子类化,您也许可以自定义它。

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

    【讨论】:

    • 你做过这个吗?任何示例代码都会很有帮助。
    • 更改按钮颜色并不难,您无需使用未记录的 API 即可。
    • 你不应该继承UIActionSheet。来自文档:UIActionSheet is not designed to be subclassed, nor should you add views to its hierarchy. If you need to present a sheet with more customization than provided by the UIActionSheet API, you can create your own and present it modally with presentViewController:animated:completion:.
    【解决方案4】:

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

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

    【讨论】:

      【解决方案5】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-30
        • 2011-01-11
        • 1970-01-01
        相关资源
        最近更新 更多