5

所以我创建了一个按钮。我希望它有圆角而不制作圆角 .png 文件我可以设置背景图像。我可以绕过角落。但是,当我设置背景图像时,圆角消失了。

我很久以前做过一次,但似乎无法让它再次工作。如何才能做到这一点?

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 addTarget:self action:@selector(goSpecials)
  forControlEvents:UIControlEventTouchUpInside];
[button1 setTitle:@"Specials" forState:UIControlStateNormal];
button1.titleLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:(15.0)];
[button1 setTitleColor:[self colorWithHexString:buttonColor] forState:UIControlStateNormal];
button1.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"button.png"]]; 
 //[button1 setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
button1.frame = CGRectMake(20.0, 232.0, 135.0, 32.0);
button1.layer.borderColor = [UIColor blackColor].CGColor;
button1.layer.borderWidth = 0.5f;
button1.layer.cornerRadius = 8.0f;
[self.view addSubview:button1];

按钮.png: 在此处输入图像描述

4

1 回答 1

18

将 maskToBounds 属性添加到您的按钮。

button1.layer.masksToBounds = YES;
于 2012-05-11T15:56:55.157 回答