2014-04-28 197 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