如何用iOS创建一个大的红色UIButton?

igu*_*222 52 objective-c uibutton uitableview ios

使用iOS,我如何创建一个类似于删除iPhone上的联系人时使用的红色"删除"按钮?

con*_*are 85

首先从可伸展的图像开始:

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

然后,您创建一个拉伸图像作为背景的按钮并应用文本.

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 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];
Run Code Online (Sandbox Code Playgroud)

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


Sta*_*eff 62

我也制作了一些按钮......视网膜和非视网膜版本

如果要在Cell中使用它们,只需在cellForRowAtIndexPath中使用以下代码:

UIButton *sampleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[sampleButton setFrame:[cell.contentView frame]];
[sampleButton setFrame:CGRectMake(0, 0, cell.bounds.size.width-20, 44)];
[sampleButton setBackgroundImage:[UIImage imageNamed:@"button_red.png"] forState:UIControlStateNormal];
[cell addSubview:sampleButton];
Run Code Online (Sandbox Code Playgroud)

绿色按钮正常

红色按钮正常

灰色按钮正常

绿色按钮视网膜 红色按钮视网膜 灰色按钮视网膜

  • @Hisoka:sampleButton.alpha = 0.5; (2认同)

yon*_*nel 22

我认为那些更好(它们在视网膜显示上看起来也很好):

替代文字 替代文字

.png从这个非常好的.psd文件生成:http://www.teehanlax.com/blog/2010/08/12/iphone-4-gui-psd-retina-display/

然后将它用作您的背景的可拉伸图像__CODE__:

__PRE__