デフォルトのiOSUIButton(ストーリーボード内)があります。
変更したい(border-radiusの削除、実線の境界線の追加、背景の変更など)。コードはどこに書くべきですか?どの方法を使用すればよいですか?どのクラスをインポートする必要がありますか?
custom
Interface Builderでボタンの種類を選択することで、すべてではないにしても、ほとんどのことを実行できます。それでもすべてが達成されない場合は、コードを使用してこれをすべて設定できます。
ファイルにあることを確認#import <QuartzCore/QuartzCore.h>
してください。.m
プロパティを設定します(このSO回答のベース):
float borderWidth = ...;
UIColor *borderColor = ...; // create the color you want
[[myButton layer] setBorderWidth:borderWidth];
[[myButton layer] setBorderColor:borderColor.CGColor];
同様の方法で、必要なすべてのボタンプロパティを確認できます。((
私の単純なコードでは、次のようなものがあります。
@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];
プロパティ定義を次のように変更する必要があります。
@property (nonatomic, strong) IBOutlet UIButton *rssButton;
そして、UIButtonオブジェクトをストーリーボードからこのプロパティに接続すると、コードを介してオブジェクトを好きなように変更できます(同じコントローラーでこれを実行していると仮定します)
ストーリーボードですでに定義されている場合は、そのボタンをビューに追加する最後の行を削除します