12

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

现在,上面代码的问题在于它没有创建看起来与 Interface builder 中默认创建的按钮相似的按钮。边界不见了。

我通过以下代码创建上述类型的按钮:

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

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

4

4 回答 4

27

您首先从带有边框的可拉伸图像开始:

替代文字 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];

显然,您需要调整框架原点和大小以匹配您的应用程序,以及目标、选择器和标题。和

于 2009-11-02T19:22:59.873 回答
6
[sampleButton setFont:[UIFont boldSystemFontOfSize:20]]; 

setFont 现在已弃用,请改用 titleLabel.font 属性

sampleButton.titleLabel.font = [UIFont boldSystemFontOfSize:20];
于 2010-06-06T09:33:07.667 回答
3

您可以将单个类用于自定义 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;
}
于 2012-10-20T06:11:54.213 回答
2

你不应该打电话initWithFrame: rect而不是:

    HopitalButton* hb = [[HopitalButton alloc] init];
于 2009-11-02T19:25:06.617 回答