如何在iPhone中使用图像背景制作按钮?

时间:2011-06-14 02:50:19

标签: objective-c xcode ios4 uibutton

我有一个圆形按钮,但我会让它有一个带图像的背景。

但是当我这样做时,通过将(what)属性设置为图像,该按钮变为矩形,因为图像是矩形。如何保持按钮轮?

3 个答案:

答案 0 :(得分:10)

只需这样做

#import <QuartzCore/QuartzCore.h>
myButton.layer.cornerRadius = 8;

答案 1 :(得分:0)

经过测试的代码:

·H

    -(void)vijayWithApple; 

的.m

   -(void)vijayWithApple{

            NSLog(@"vijayWithApple Called");
   }


UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

[button setImage:[UIImage imageNamed:@"Micky.png"] forState:UIControlStateNormal];

[button addTarget:self action:@selector(vijayWithApple) forControlEvents:UIControlEventTouchUpInside];

[button setTitle:@"Show View" forState:UIControlStateNormal];

button.frame = CGRectMake(135.0, 180.0, 40.0, 40.0);//width and height should be same value

button.clipsToBounds = YES;

button.layer.cornerRadius = 20;//half of the width

button.layer.borderColor=[UIColor redColor].CGColor;

button.layer.borderWidth=2.0f;

[self.view addSubview:button];

<强> Restut

enter image description here

答案 2 :(得分:0)

我特别为UITableCell找到的最佳解决方案。我将代码放在awakeFromNib中,然后立即开始工作。