2013-03-14 52 views
0

我希望在整個應用程序中使用我的UIButton。我試圖使用UIAppearance協議,但它並沒有爲UIButton公開任何有趣的方法。我想要更改所有UIButton的textColor和字體。UIButton全球外觀

任何想法?

+0

我可以建議你閱讀[設計爲iOS:馴服的UIButton(http://robots.thoughtbot.com/post/33427366406/designing-for-ios-taming-uibutton)和[創建時尚UIButtons的五個技巧](http://mobile.tutsplus.com/tutorials/iphone/custom-uibutton_iphone/)。它將爲您提供如何定製UIBUtton的完整概述,包括您想要的屬性。 – o15a3d4l11s2 2013-03-14 15:17:08

回答

2

只是子類UIButton並覆蓋自定義初始化或便利工廠方法之一。事情是這樣的:

@interface MyCustomButton: UIButton 
@end 

@implementation MyCustomButton 

+ (UIButton *)buttonWithType:(UIButtonType)type 
{ 
    UIButton *b = [super buttonWithType:type]; 
    b.titleLabel.textColor = [UIColor redColor]; 
    b.titleLabel.font = [UIFont fontWithName:@"FooBar-Bold" size:3.14]; // today's Pi day 
    return b; 
} 

@end 
+0

謝謝!我想在繼承後,我必須去每一次出現的UIButton,並將其超類更改爲MyCustomButton。有沒有更簡單的方法來說,我的所有UIButtons都將從MyCustomButton派生。我想沒有更簡單的方法。謝謝! – 2013-03-14 15:28:37