2014-04-28 43 views
-4

我正在寻找简单的方法来设置UIButton's默认titleColor在应用程序启动时自定义一个。但我想尊重这种情况,titleColor不是默认值并且具有自定义值。 [[UIButton appearance] setTitleColor: forState:]允许我一次为所有按钮设置自定义颜色。但它覆盖已设置的自定义颜色。 有没有快速的方式来覆盖默认标题颜色和尊重自定义颜色在同一时间?更改UIButton的默认标题颜色

+0

[button setTitleColor:[UIColor YourColor] forState:UIControlStateNormal];请在以这种方式提问 – TamilKing

+0

之前搜索你的ans,我需要每次在每个控制器上编程设置按钮的颜色。但我希望在应用程序启动时一次性使用默认颜色。但就此而言,如果已经在界面构建器中将颜色设置为自定义值(不是默认颜色选项),则不应该覆盖自定义颜色。 –

回答

0

使用appearance为您设置全局外观(所有按钮),并实例方法[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];用于设置颜色只是一个按钮来完成。

+0

@nyekimov这是你所需要的? – damirstuhec

+0

是的,伙计。这真的是我需要的。我尝试通过界面生成器自定义颜色,显然它的外观如此丰富。但是,您的程序性建议适用于葡萄酒。 –

+0

@nyekimov我很高兴听到这一点。如果它有效,那么你应该接受我的答案。 – damirstuhec

0
Try to this..... 

    create catagory class for UIButton like bellow 

    Sample.h 
    #import <UIKit/UIKit.h> 

    @interface Sample : UIButton 

    @end 

    Sample.m 
    #import "Sample.h" 

    @implementation Sample 
    -(id)initWithCoder:(NSCoder *)aDecoder 
    { 
     self=[super initWithCoder:aDecoder]; 
     if (self) { 
      [self setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; 
     } 
     return self; 
    } 
    - (id)initWithFrame:(CGRect)frame 
    { 
     self = [super initWithFrame:frame]; 
     if (self) { 
      // Initialization code 
     } 
     return self; 
    } 

    @end 


    and After crate class use this class as Custom Class Like 

![Image][1] 


    [1]: http://i.stack.imgur.com/sXYfY.png