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

推荐答案

首先从可拉伸图像开始:

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

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

Objective-c相关问答推荐

iOS中的非延迟图像加载

UIApplication.sharedApplication.delegate.window 和 UIApplication.sharedApplication.keyWindow 有什么区别?

self.ivar 和 ivar 之间的区别?

Asihttprequest 61 错误

UIGestureRecognizer 是否适用于 UIWebView?

来自 NSString 的 NSJSONSerialization

重复符号 _OBJC_METACLASS_$_ClassName

找不到名为ProjectName的目标

iOS7过度导航栏按钮填充

Objective C 静态类变量

在 NSMutableArray 的开头添加一个对象?

滚动后检测 UITableView 中的当前顶部单元格

如何在情节提要场景中嵌入自定义视图 xib?

在 Objective C 中使用 extern

Objective-C 的 NSMutableArray 是线程安全的吗?

隐藏 NSTableView 标头?

使用 UINavigationController 在 UIPageViewController 中下推内容

iPhone如何在打字时获取UITextField的文本?

如何删除 UIToolBar 上的顶部边框

哪个是正确的,nil 或 NULL,来标记没有 Objective-C 块?