UIActionSheet button's color

后端 未结 5 970
失恋的感觉
失恋的感觉 2020-11-29 06:27

How can I change the color of UIActionSheet button\'s color?

相关标签:
5条回答
  • 2020-11-29 06:34

    I have created child class UICustomActionSheet, which allows customize fonts, colors and images of buttons inside UIActionSheet. It is absolutely safety for appstore, you can find code of this class on next link:

    https://github.com/gloomcore/UICustomActionSheet

    Enjoy it!

    0 讨论(0)
  • 2020-11-29 06:36

    Unfortunately, without using undocumented API's, there is no official way to change the button's color on a UIActionSheet. You may be able to customize this if you subclass the UIActionSheet control.

    See this example: http://blog.corywiles.com/customizing-uiactionsheet-buttons

    0 讨论(0)
  • 2020-11-29 06:38

    You can easily achieve it by using following code

    Apple UIActionSheetDelegate protocol documentation

    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];
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-29 06:39

    iOS 8 (UIAlertController)

    It's super simple to do if you're using a UIAlertController. Simply change the tint color on the view of the UIAlertController.

    [alertController.view setTintColor:[UIColor red];
    

    iOS 7 (UIActionSheet)

    I successfully change the text color by using this simple method.

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

    Make sure to run this AFTER you call

    [actionSheet showInView];
    

    If you call it before [showInView], all buttons but the cancel button will be colored. Hope this helps someone!

    0 讨论(0)
  • 2020-11-29 06:47

    We can use background images to do it. I think it is the easiest way.

    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 讨论(0)
提交回复
热议问题