Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone的自定义UI按钮_Iphone_Uibutton - Fatal编程技术网

Iphone的自定义UI按钮

Iphone的自定义UI按钮,iphone,uibutton,Iphone,Uibutton,我在我的应用程序中有一个视图,该视图根据服务器返回的项目数有许多按钮。因此,如果服务器返回10个项目,那么应该有10个按钮,单击每个按钮应该呼叫不同的人 出于上述目的,我创建了一个从UIButton派生的自定义button类 @implementation HopitalButton @synthesize index; @synthesize button_type; - (id)initWithFrame:(CGRect)frame { if (self = [super in

我在我的应用程序中有一个视图,该视图根据服务器返回的项目数有许多按钮。因此,如果服务器返回10个项目,那么应该有10个按钮,单击每个按钮应该呼叫不同的人

出于上述目的,我创建了一个从UIButton派生的自定义button类

@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];
我找不到为这个自定义按钮设置按钮类型的方法。
有什么办法我能做到吗?或者有没有更好的方法来设计代码。

首先从一个带边框的可拉伸图像开始:

然后制作一个以拉伸图像为背景的按钮,并应用文本

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];
INDEX\u OFFSET=82753;//随机的
UIButton*sampleButton=[UIButton Button类型:UIButtonTypeCustom];
[sampleButton设置框架:CGRectMake(kLeftMargin,10,self.view.bounds.size.width-kLeftMargin-kRightMargin,52)];
[样本按钮设置标题:@“按钮标题”用于状态:UIControlStateNormal];
[sampleButton setFont:[UIFont boldSystemFontOfSize:20];
[sampleButton设置标签:+INDEX_OFFSET];
[sampleButton setBackgroundImage:[[UIImage ImageName:@“redButton.png”]可拉伸图像,带左CapWidth:10.0 topCapHeight:0.0]用于状态:UIControlStateNormal];
[sampleButton addTarget:self action:@selector(按下按钮)for ControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:sampleButton];

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

您不应该调用
initWithFrame:rect而不是:

    HopitalButton* hb = [[HopitalButton alloc] init];
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

但是索引对我的程序很重要,因为我使用索引来找出哪个按钮被按下,因为视图中有不止一个按钮。你从哪里得到了可拉伸的红色按钮图像?我想我很久以前从某个教程中抓到了它……但在photoshop中制作它应该不会太难
#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
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;
}