【发布时间】:2011-09-14 09:05:56
【问题描述】:
我有一个圆形按钮,但我会让它有一个带图像的背景。
但是当我这样做时,通过将 (what) 属性设置为图像,按钮变为矩形,因为图像是矩形。如何使按钮保持圆形?
【问题讨论】:
-
为什么不使用圆形图像?
标签: objective-c xcode ios4 uibutton
我有一个圆形按钮,但我会让它有一个带图像的背景。
但是当我这样做时,通过将 (what) 属性设置为图像,按钮变为矩形,因为图像是矩形。如何使按钮保持圆形?
【问题讨论】:
标签: objective-c xcode ios4 uibutton
我发现的最佳解决方案尤其适用于 UITableCell。我将代码放入 awakeFromNib 并立即工作。
【讨论】:
测试代码:
.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];
恢复
【讨论】:
只需这样做
#import <QuartzCore/QuartzCore.h>
myButton.layer.cornerRadius = 8;
【讨论】: