質問
私は、サーバーから返されたアイテムの数に基づいて、ボタンの数を持っている私のアプリでビューを持っています。サーバーが返す10の項目を言うのであれば、そこに10個のボタンであるべきで、各ボタンをクリックすると、別の人を呼び出す必要があります。
上記の目的のために、私はUIButtonから派生するカスタムボタンのクラスを作成します。
@implementation HopitalButton
@synthesize index;
@synthesize button_type;
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
UIImage* img = [UIImage imageNamed:@"dr_btn.png"];
[img stretchableImageWithLeftCapWidth:10 topCapHeight:10];
[self setBackgroundImage:img forState:UIControlStateNormal];
[self setTitleColor:[UIColor colorWithRed:0.698 green:0.118 blue:0.376 alpha:1] forState:UIControlStateNormal] ;
[self setFont:[UIFont fontWithName:@"Helvetica Bold" size:13]];
self.titleLabel.textColor = [UIColor colorWithRed:178 green:48 blue:95 alpha:1];
self.adjustsImageWhenHighlighted = YES;
}
return self;
}
- (void)dealloc {
[super dealloc];
}
@end
さて、上記のコードの問題は、それがインターフェイスビルダーにデフォルトで作成したボタンのようになり、ボタンを作成していないということです。国境が不足しています。
そして、私は次のコードで上記のタイプのボタンを作成します:
HopitalButton* hb = [[HopitalButton alloc] init];
hb.button_type = @"call";
hb.frame = CGRectMake(50, 50 + i * 67, 220, 40);
[self.scroll_view addSubview:hb];
[hb setTitle:[[[self.office_full_list objectAtIndex:i] objectForKey:@"Staff" ]objectForKey:@"FullName"] forState:UIControlStateNormal];
hb.index = [NSNumber numberWithInt:[self.button_items count]];
[self.button_items insertObject:hb atIndex:[self.button_items count]];
[hb addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
私は、このカスタムボタンのボタンの種類を設定する方法を見つけることではないのです。 私はそれを行うことができます方法はありますか?またはコードを設計するためのより良い方法があります。
解決
あなたが最初のボーダーで伸縮性の画像で始まります:
そして、あなたが背景として、延伸画像でボタンを作成し、テキストを適用します。
INDEX_OFFSET = 82753; // random
UIButton *sampleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[sampleButton setFrame:CGRectMake(kLeftMargin, 10, self.view.bounds.size.width - kLeftMargin - kRightMargin, 52)];
[sampleButton setTitle:@"Button Title" forState:UIControlStateNormal];
[sampleButton setFont:[UIFont boldSystemFontOfSize:20]];
[sampleButton setTag:<INDEX>+INDEX_OFFSET];
[sampleButton setBackgroundImage:[[UIImage imageNamed:@"redButton.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
[sampleButton addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:sampleButton];
もちろん、アプリだけでなく、ターゲット、セレクタ、およびタイトルに一致するようにフレームの原点とサイズを調整する必要があります。そして、
他のヒント
[sampleButton setFont:[UIFont boldSystemFontOfSize:20]];
のsetFontが廃止され、代わりにtitleLabel.fontプロパティを使用します。
sampleButton.titleLabel.font = [UIFont boldSystemFontOfSize:20];
は、
以下のように特定のフレームスタイルでプロジェクト全体に有用であることができるカスタムRoundrectボタンの個々のクラスを使用することができます#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface CustomRoundRectButton : UIButton
@end
#import "CustomRoundRectButton.h"
@implementation CustomRoundRectButton
- (void)drawRect:(CGRect)rect
{
[[self layer] setMasksToBounds:YES];
[self.layer setCornerRadius:10.0f];
[self.layer setBorderColor:[UIColor grayColor].CGColor];
[self.layer setBorderWidth:1.0];
}
@end
このでは、ボタン型のカスタムを選択し、CustomRoundRectButtonとしてそのクラスを選択する必要があります。
For Simple custom button we can use as below
-(UIBarButtonItem*)BackButton
{
UIButton*btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
[btn setFrame:CGRectMake(0, 0, 30, 30)];
[btn addTarget:self action:@selector(actionBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem*barBtn = [[[UIBarButtonItem alloc] initWithCustomView:btn] autorelease];
return barBtn;
}
あなたはinitWithFrame: rect
を呼び出す代わりにしてはいけません。
HopitalButton* hb = [[HopitalButton alloc] init];
所属していません StackOverflow