使用iOS,我如何创建一个类似于删除iPhone上的联系人时使用的红色“删除”按钮? 首先从可伸展的图像开始: alt text http://grab.by/4lP 然后,您创建一个拉伸图像作为背景的按钮并应用文本
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];
显然,您需要调整框架原点和大小以匹配您的应用程序,以及目标,选择器和标题.