2013-02-25 66 views
0

我有一個默認的iOS UIButton(故事板)。修改按鈕的風格iOS

我想改變它(刪除邊框半徑,添加實心邊框,更改背景等)。我應該在哪裏編寫代碼?我應該使用哪種方法?我應該導入哪些類?

回答

2

您可以通過在界面B中爲按鈕類型選擇custom來做大多數(如果不是全部的話) uilder。如果沒有完成所有工作,您可以通過代碼設置所有這些:

確保您在.m文件中有#import <QuartzCore/QuartzCore.h>

設置屬性(在this SO answer基地):

float borderWidth = ...; 
UIColor *borderColor = ...; // create the color you want 

[[myButton layer] setBorderWidth:borderWidth]; 
[[myButton layer] setBorderColor:borderColor.CGColor]; 

你可以通過你以同樣的方式希望所有按鈕的屬性。 (

+0

感謝它幫我! – 2013-02-25 17:43:47

0

在我簡單的代碼,我有這樣的事情:

@property (nonatomic, strong) UIButton *rssButton; 

然後,我在執行定義這個按鈕:

- (UIButton *)rssButton { 
    if(_rssButton == nil) { 
     _rssButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     _rssButton.frame = CGRectMake(60, 200, 200, 40); 
     [_rssButton setTitle:@"Get newest RSS article" forState:UIControlStateNormal]; 
     _rssButton.titleLabel.textColor = [UIColor colorWithRed:0.294 green:0.553 blue:0.886 alpha:1]; 
     _rssButton.backgroundColor = [UIColor whiteColor]; 

     _rssButton.layer.borderColor = [UIColor blackColor].CGColor; 
     _rssButton.layer.borderWidth = 0.5f; 
     _rssButton.layer.cornerRadius = 10.0f; 

     [_rssButton addTarget:self action:@selector(getDataFromRSS) forControlEvents:UIControlEventTouchUpInside]; 
    } 
    return _rssButton; 
} 

而旁邊添加在你的主要觀點:

[self.view addSubview:self.rssButton]; 
0

您需要將您的屬性定義改成這樣:

@property (nonatomic, strong) IBOutlet UIButton *rssButton; 

而且從故事板到這個屬性連接您的UIButton對象,那麼你將能夠改變物體,你等等,雖然代碼(假設你正在做的這在同一個控制器)

,並刪除你的最後一行在其中添加該按鈕,您的看法,如果它在你的故事板中已經定義