2009-11-02 71 views
12

我在我的應用程序中有一個視圖,它有一些基於服務器返回的項目數量的按鈕。因此,如果服務器返回說10個項目,應該有10個按鈕,點擊每個按鈕應該調用一個不同的人。Iphone的自定義UIButton

爲了上述目的,我創建了一個從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]; 

我沒有找到一個方法來設置這個自定義按鈕按鈕類型。 有沒有辦法可以做到這一點?或者有更好的方法來設計代碼。

回答

27

你先用一個邊界伸展圖像開始:

alt text http://grab.by/4lP

然後你犯了一個按鈕,用拉伸圖像作爲背景,並應用文字。

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]; 

顯然,您需要調整幀的來源和大小以匹配您的應用以及目標,選擇器和標題。而

+0

但是當我使用索引來找出哪個按鈕被按下,因爲有在視圖中 – Amal 2009-11-02 19:32:05

+0

多個按鈕設置標籤按鈕的索引是我的程序很重要。我更新了代碼 – coneybeare 2009-11-02 19:39:39

+0

你從哪裏得到可拉伸的紅色按鈕圖像? – bentford 2009-11-03 01:22:52

2

你不是應該叫initWithFrame: rect代替:

HopitalButton* hb = [[HopitalButton alloc] init]; 
6
[sampleButton setFont:[UIFont boldSystemFontOfSize:20]]; 

setfont程序現在已經過時了,用titleLabel.font屬性,而不是

sampleButton.titleLabel.font = [UIFont boldSystemFontOfSize:20]; 
3

您可以使用單獨的類定製圓形按鈕,可以在整個項目中使用,具體框架如下:

#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; 
}