使用正方形背景图片的圆角UIButton

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

button.png: enter image description here

1个回答

18

给你的按钮添加maskToBounds属性。

button1.layer.masksToBounds = YES;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接