Custom UIButton for Iphone(Iphone 的自定义 UIButton)
问题描述
我的应用中有一个视图,其中有许多按钮,这些按钮基于服务器返回的项目数.因此,如果服务器返回 10 个项目,则应该有 10 个按钮,并且单击每个按钮应该调用不同的人.
I have an view in my App which has a number of buttons based on the number of items returned by the server. So if the server returns say 10 items, there should be 10 buttons and clicking on each button should call a different person.
出于上述目的,我创建了一个从 UIButton 派生的自定义按钮类.
For the above purpose I created a custom button class deriving from 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
现在上面代码的问题是它没有创建看起来与界面生成器中默认创建的按钮相似的按钮.边框不见了.
Now the problem with the above code is that it does not create buttons that look similar to the buttons created by default in Interface builder. The borders are missing.
我通过以下代码创建上述类型的按钮:
And I create buttons of the above type by the following code:
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];
我没有找到为此自定义按钮设置按钮类型的方法.有什么办法可以做到吗?或者有没有更好的方法来设计代码.
I am not finding a way to set the button type for this custom button. Is there a way i can do it ? Or is there a better way to design the code.
推荐答案
你首先从一个带边框的可拉伸图像开始:
You first start with a stretchable image with a border:
替代文字 http://grab.by/4lP
然后你制作一个以拉伸图像为背景的按钮并应用文本.
Then you make a button with the stretched image as the background and apply text.
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];
显然,您需要调整框架原点和大小以匹配您的应用,以及目标、选择器和标题.还有
Obviously, you will need to adjust the frame origin and size to match your app, as well as the target, selector, and title. And
这篇关于Iphone 的自定义 UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Iphone 的自定义 UIButton
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01