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消息框 iOS7 操作表

4

7 回答 7

12

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

[[UIView appearance] setTintColor:[UIColor whiteColor]];
于 2015-02-03T21:23:40.803 回答
11

正如UIAlertView已弃用你可以。使用UIAlertController.

你可以使用tintColor属性。

老的

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

-来自 Apple Doc

你可以使用tintColor属性或者你可以使用一些自定义库,你可以在 cocoacontrols.com 找到它。

于 2013-11-15T06:43:03.823 回答
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];
        }
    }
}
于 2013-11-15T06:50:37.267 回答
3

结合上面的最佳答案,并更新为弃用:

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

或斯威夫特:

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

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

于 2018-11-30T19:38:36.190 回答
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它返回给定类或子类的完整子视图层次结构中的视图数组。

于 2014-07-08T14:44:42.293 回答
1

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

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

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

于 2014-09-23T11:35:51.297 回答
-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];
于 2014-02-01T06:41:31.167 回答