更改UIAlertview和UIActionsheet按钮的色调颜色

9

我正在尝试适配我的应用程序到iOS 7。我遇到的问题是我无法更改某些控件的色调颜色。

我确实添加了

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
if (IOS7_OR_LATER)
    self.window.tintColor = [self greenTintColor];

到我的应用程序委托

           - (BOOL)application:(UIApplication *)application
 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

它大多数时候是有帮助的,但是消息框和操作表按钮的颜色仍然是默认的蓝色。

我该如何重新着色所有这样的按钮呢?

一些截图:

iOS7 message box iOS7 action sheet

7个回答

11

我能在应用程序委托中将取消按钮的文本颜色更改为白色。

[[UIView appearance] setTintColor:[UIColor whiteColor]];

救了我的一天!必须作为答案! - ad1Dima
6
嗯,它做得比应该做的多。我更愿意使用[[UIView appearanceWhenContainedIn:[UIAlertView class], nil] setTintColor:[UIColor whiteColor]];[[UIView appearanceWhenContainedIn:[UIAlertController class], nil] setTintColor:[UIColor whiteColor]]; - Kévin Renella
我更倾向于 @KévinRenella 的答案。Ali的答案非常危险。 - Kenn Cal

11

由于 UIAlertView 已经被弃用,您可以使用 UIAlertController

您可以使用 tintColor 属性。

旧版:

UIAlertView 类旨在直接使用,不支持子类化。该类的视图层次结构是私有的,不得进行修改。

-来自苹果文档

您可以使用 tintColor 属性,或者您可以使用一些自定义库实现此功能,您可以在 cocoacontrols.com 上找到它。


5
在iOS 8中,有一个警告视图的属性alert.view.tintColor = UIColor.redColor()。 - Zeezer
答案已从https://dev59.com/iWIk5IYBdhLWcg3wg-jK#19198435复制,至少更改文本! - Tarek Hallak

6

对于操作表,您可以使用UIActionSheet的willPresentActionSheet委托方法来更改操作表按钮的颜色。

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
    for (UIView *subview in actionSheet.subviews) {
        if ([subview isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)subview;
            button.titleLabel.textColor = [UIColor greenColor];
        }
    }
}

然而,这种变化只是暂时的。如果您点击并按住一个按钮,然后在其外部释放触摸,颜色会恢复为默认颜色。实际上,它已经在触摸下降时改变为默认颜色。 - pre
1
调用[button setTitleColor:forControlState:]以使其持久化。 - John Gibb

3

结合以上最佳答案,并根据弃用进行更新:

[[UIView appearanceWhenContainedInInstancesOfClasses:@[[UIAlertController class]]] setTintColor:[UIColor greenColor]];

或者Swift:
UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = .green

适用于2018年,Swift 4 / iOS 12。


1
您可以通过搜索并修改显示警报后创建的警报窗口的子视图层次结构中的UILabel来调整颜色:
- (void)setButtonColor:(UIColor*)buttonColor {
    dispatch_after(dispatch_time(0,1), dispatch_get_main_queue(), ^{
        NSMutableArray *buttonTitles = [NSMutableArray array];
        for (NSUInteger index = 0; index < self.numberOfButtons; index++) {
            [buttonTitles addObject:[self buttonTitleAtIndex:index]];
        }
        for (UILabel *label in [[[UIApplication sharedApplication] keyWindow] recursiveSubviewsOfKind:UILabel.class]) {
            if ([buttonTitles containsObject:label.text]) {
                label.textColor = buttonColor;
                label.highlightedTextColor = buttonColor;
            }
        }
    });
}

[alert show];
[alert setButtonColor:UIColor.redColor];

recursiveSubviewsOfKind: 方法是 UIView 的一个分类,它返回给定类或子类完整子视图层次结构中的视图数组。


谢谢。这适用于 UIAlertView,但也有例如 UIBarButtonItem 的情况。对于 UIBarButtonItem,效果是暂时的:颜色在点击后或按住按钮后会恢复为默认值。 - Bobrovsky

1

谢谢。看起来很有前途。 - Bobrovsky

-3

在iOS 6.0中,在应用程序委托中创建自定义视图

.h
UIView* _loadingView;
    UIView* _subView;
    UIActivityIndicatorView*loadingIndicator;
    UITabBarController *tabBar_Controller;
    NSTimer *timer;


@property (strong, nonatomic) UIView* _loadingView;
@property (strong, nonatomic) UIView* _subView;



.m- (void)fadeScreen
{
    [UIView beginAnimations:nil context:nil]; // begins animation block
    [UIView setAnimationDuration:3.0]; // sets animation duration
    [UIView setAnimationDelegate:self]; // sets delegate for this block
    [UIView setAnimationDidStopSelector:@selector(finishedFading)];
    self.txtview.alpha = 0.0;  // Fades the alpha channel of this view
    [UIView commitAnimations];  // commits the animation block.  This
}



- (void) finishedFading
{
    [self.txtview removeFromSuperview];
}




- (void)showConnectivity:(NSString *)strTitle
{
    [_loadingView setBackgroundColor:[UIColor clearColor]];
    [_loadingView setAlpha:0.5];
    [_loadingView.layer setCornerRadius:10];
    [self.window addSubview:_loadingView];
    [_loadingView setHidden:NO];

    [_subView.layer setCornerRadius:7];
    [_subView setBackgroundColor:[UIColor colorWithHue:0.0f saturation:0.0f brightness:0.0f alpha:0.6]];
    [_subView setOpaque:YES];
    [self.window addSubview:_subView];
    [_subView setHidden:NO];

    [_loadingView setHidden:NO];
    [_subView setHidden:NO];

    loadingIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [loadingIndicator setFrame:CGRectMake(85,10,35,35)];
    [_subView addSubview:loadingIndicator];
    [loadingIndicator setBackgroundColor:[UIColor redColor]];
    [loadingIndicator startAnimating];

    UILabel *_lab=[[UILabel alloc]initWithFrame:CGRectMake(8,10,72,45)];
    [_lab setText:strTitle];
    [_lab setTextColor:[UIColor whiteColor]];
    [_lab setBackgroundColor:[UIColor clearColor]];
    [_lab setFont:[UIFont boldSystemFontOfSize:13.0]];
    [_lab setTextAlignment:NSTextAlignmentCenter];
    [_subView addSubview:_lab];


}

- (void)CoonectingViewHidden
{

    [_loadingView setHidden:YES];
    [_subView setHidden:YES];

    NSArray *_aryViews = [_subView subviews];
    for(int i = 0; i<[_aryViews count];i++)
    {
        id obj = [_aryViews objectAtIndex:i];
        if(![obj isKindOfClass:[UIActivityIndicatorView class]])
            [obj removeFromSuperview];
    }
    [loadingIndicator stopAnimating];
    [loadingIndicator hidesWhenStopped];

}



in using .m
#import"Appdelegate.h"

- (void)showLoadingIndicator:(NSString *)message
{
    AppDelegate *delegateObj2=(AppDelegate *)[UIApplication sharedApplication].delegate;
    [delegateObj2 showConnectivity:message];
}
-(void)stopLoading
{
    AppDelegate *delegateObj3=(AppDelegate *)[UIApplication sharedApplication].delegate;
    [delegateObj3 CoonectingViewHidden];

}



// [self showLoadingIndicator:@"Loading"];
n

[self stopLoading];

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