Iphone的自定义UIButton

问题描述:

我在我的应用程序中有一个视图,它有一些基于服务器返回的项目数量的按钮。因此,如果服务器返回说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]; 

我没有找到一个方法来设置这个自定义按钮按钮类型。 有没有办法可以做到这一点?或者有更好的方法来设计代码。

你先用一个边界伸展图像开始:

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

你不是应该叫initWithFrame: rect代替:

HopitalButton* hb = [[HopitalButton alloc] init]; 

[sampleButton setFont:[UIFont boldSystemFontOfSize:20]]; 

setfont程序现在已经过时了,用titleLabel.font属性,而不是

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

您可以使用单独的类定制圆形按钮,可以在整个项目中使用,具体框架如下:

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