Ios 更改UIAlertview和UIActionsheet按钮的色调颜色
我正在尝试将我的应用程序调整为iOS 7。我遇到的问题是我无法更改某些控件的着色颜色 我确实加了一句Ios 更改UIAlertview和UIActionsheet按钮的色调颜色,ios,objective-c,cocoa-touch,uialertview,uiactionsheet,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]; 到我的应用程序代理的 - (BOOL)appli
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
这主要是有帮助的,但消息框和操作表按钮的颜色仍然是默认的蓝色
我怎样才能回忆起所有这些按钮呢
一些截图:
由于
UIAlertView
已被弃用,您可以。使用UIAlertController
您可以使用tintColor
属性
旧的
UIAlertView类旨在按原样使用,而不是按原样使用
支持子类化。此类的视图层次结构是私有的,并且
不能修改
-来自苹果医生
您可以使用tintColor属性,也可以使用一些自定义库,您可以在cocoacontrols.com上找到它。有关您可以使用的操作表 利用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];
}
}
}
在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];
.h
UIView*_加载视图;
UIView*\u子视图;
UIActivityIndicatorView*加载指示器;
UITabBarController*tabBar\u控制器;
n定时器*定时器;
@属性(强,非原子)UIView*\u loadingView;
@属性(强,非原子)UIView*_子视图;
.m-(无效)淡入淡出淡出淡出淡出淡出淡出淡出淡出淡出淡出淡出淡出淡出淡出淡出淡出淡出淡出淡出淡出淡出淡出淡出淡出淡出淡出
{
[UIView beginAnimations:nil上下文:nil];//开始动画块
[UIView setAnimationDuration:3.0];//设置动画持续时间
[UIView setAnimationDelegate:self];//设置此块的委托
[UIView setAnimationDidStopSelector:@selector(完成加载)];
self.txtview.alpha=0.0;//淡入此视图的alpha通道
[UIView commitAnimations];//提交动画块。此
}
-(无效)已完成的加载
{
[self.txtview从SuperView移除];
}
-(void)showConnectivity:(NSString*)strTitle
{
[\u loadingView setBackgroundColor:[UIColor clearColor]];
[_loadingviewsetalpha:0.5];
[_loadingView.layer setCornerRadius:10];
[self.window addSubview:_loadingView];
[_loadingviewsethidden:否];
[_subView.layer setCornerRadius:7];
[_子视图setBackgroundColor:[UIColor-With色调:0.0f饱和度:0.0f亮度:0.0f alpha:0.6];
[_子视图set不透明:是];
[self.window addSubview:_subView];
[_子视图集隐藏:否];
[_loadingviewsethidden:否];
[_子视图集隐藏:否];
loadingIndicator=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[loadingIndicator setFrame:CGRectMake(85,10,35,35)];
[_subviewaddsubview:loadingIndicator];
[loadingIndicator setBackgroundColor:[UIColor redColor]];
[装载指示器开始启动];
UILabel*_lab=[[UILabel alloc]initWithFrame:CGRectMake(8,10,72,45)];
[u lab setText:strTitle];
[_labsettextcolor:[UIColor whiteColor]];
[_LabSetbackgroundColor:[UIColor clearColor]];
[_LabSetFont:[UIFont boldSystemFontOfSize:13.0];
[_labsettextalignment:NSTextAlignmentCenter];
[_subviewaddsubview:_lab];
}
-(无效)CooConnectionViewHidden
{
[_loadingviewsethidden:是];
[_子视图集隐藏:是];
NSArray*_aryViews=[_子视图子视图];
对于(int i=0;i您可以通过在显示警报后立即创建的警报窗口的子视图层次结构中搜索和修改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];
-(void)设置按钮颜色:(UIColor*)按钮颜色{
调度时间(0,1),调度获取主队列()^{
NSMutableArray*ButtonTiles=[NSMutableArray];
对于(整数索引=0;索引
recursiveSubviewsOfKind:
方法是UIView
上的一个类别,它返回给定类或子类的完整子视图层次结构中的一个视图数组。对于带有彩色按钮的UIAlertView,您可以使用cocoapod“SDCAlertView”
关于椰子荚:
如何安装CocoaPods:我能够在应用程序代理中将取消按钮的文本颜色更改为白色
[[UIView appearance] setTintColor:[UIColor whiteColor]];
结合以上最佳答案,并针对反对意见进行更新:
[[UIView appearanceWhenContainedInInstancesOfClasses:@[[UIAlertController class]]] setTintColor:[UIColor greenColor]];
或Swift:
UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = .green
在2018年Swift 4/iOS 12中生效。但是,此更改只是暂时的。如果您按住按钮并在其外部释放触摸,颜色将变回默认颜色。事实上,触摸时颜色已变回默认颜色。请致电[按钮设置标题颜色:forControlState:
改为持续。谢谢。这适用于UIAlertView
但也有UIBarButtonItem
s,例如。对于UIBarButtonItem
而言,效果是暂时的:点击或按住按钮后颜色恢复为默认值。谢谢。它看起来很有希望。在iOS 8中存在alert.view.tintColor=UIColor.redColor()答案已从中复制,至少更改文本!保存了我的一天!必须在答案中!嗯,它做的比它应该做的更多。我宁愿使用[[UIView appearancewhen containedin:[UIAlertView class],nil]setTintColor:[UIColor wheetcolor]];
和[[UIView appearancewhen contained in:[UIAlertController class],nil]setTintColor:[UIColor whiteColor]];
我更喜欢@KévinRenella的答案。阿里的答案非常危险。@KévinRenella你改进后的答案的快速版本是什么?@DanielSpringer即使我相信你自己也能找到;UIView.been