set cornerRadius and setbackgroundimage to UIButton set cornerRadius and setbackgroundimage to UIButton ios ios

set cornerRadius and setbackgroundimage to UIButton


Here is how I would create a button through code and set its background color.

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];btn.frame = CGRectMake(100, 100, 100,50);[btn setTitle:@"Hello" forState:UIControlStateNormal];[btn setBackgroundColor:[UIColor colorWithRed:128.0/255.0f green:0.0/255.0f  blue:0.0/255.0f alpha:0.7]];btn.frame = CGRectMake(100.0, 100.0, 120.0, 50.0);//width and height should be same  valuebtn.clipsToBounds = YES;btn.layer.cornerRadius = 20;//half of the widthbtn.layer.borderColor=[UIColor redColor].CGColor;btn.layer.borderWidth=2.0f;[self.view addSubview:btn];

Below is the image of the button that is related with the above codeenter image description here

You can always play around with code and create the colors that you need for background and border. Hope this would help you out.


btn.clipsToBounds = YES; 

i Just added this and it worked for me. I was actually able to set corner radius to UIButton with setting an image to that particular UIButton.


You can also have:

btn.clipsToBounds = true;