Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton with rounded corners using square setBackgroundImage

Tags:

ios

iphone

So I create a button. I want it to have rounded corners with out making rounded .png files I can set the background image ok. I can round the corners ok. However, when I set the background Image the rounded corners go away.

I did this once before a long time ago but can not seem to get it working again. How can this be done?

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

like image 488
Rick Avatar asked May 11 '12 15:05

Rick


1 Answers

Add the maskToBounds property to your button.

button1.layer.masksToBounds = YES;
like image 127
user814712 Avatar answered Nov 15 '22 12:11

user814712