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

时间:2013-11-15 06:38:43

标签: ios objective-c cocoa-touch uialertview uiactionsheet

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

我确实添加了

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

到我的app delegate's

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

它主要有所帮助,但消息框和操作表按钮的颜色仍然是默认的蓝色。

如何重新着色所有这些按钮?

一些截图:

iOS7 message box iOS7 action sheet

7 个答案:

答案 0 :(得分:10)

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

您可以使用 tintColor 属性。

OLD

  

UIAlertView类旨在按原样使用,但不是   支持子类化。此类的视图层次结构是私有的   不得修改。

- 来自Apple Doc

您可以使用 tintColor 属性,也可以使用某些自定义库,您可以在cocoacontrols.com找到它。

答案 1 :(得分:10)

我可以在app delegate中将取消按钮的文字颜色更改为白色。

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

答案 2 :(得分:6)

对于Actionsheet您可以使用

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

答案 3 :(得分:2)

结合以上最佳答案,并已针对弃用进行了更新:

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

或Swift:

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

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

答案 4 :(得分: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上的一个类别,它返回给定类或子类的完整子视图层次结构中的视图数组。

答案 5 :(得分:1)

对于带有彩色按钮的UIAlertView,你可以使用cocoapod“SDCAlertView”

关于CocoaPods:http://www.cocoapods.org

如何安装CocoaPods:https://www.youtube.com/watch?v=9_FbAlq2g9o&index=20&list=LLSyp50_buFrhXC0bqL3nfiw

答案 6 :(得分:-3)

在iOS 6.0中,在App委托中创建自定义视图

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