Search code examples
iphoneobjective-ciosuibuttoncalayer

UIButton With Rounded Corner On Top


I need a UIButton with backgound image,rounded corner on top and shadow.Here is my code

I tried many post from stack overflow,but none worked for me.

I was able to add UIButton having background image with rounded corners and shadow using following.

CALayer *imageLayer = [imageButton layer];
imageLayer.frame =  CGRectMake(110, 190, 100, 100);
imageLayer.cornerRadius = 10.0; 
imageLayer.shadowColor = [UIColor grayColor].CGColor;
imageLayer.shadowOpacity = 1.0;
imageLayer.shadowRadius = 1;
imageLayer.shadowOffset = CGSizeMake(03.0f, 08.0f);
imageLayer.masksToBounds = YES;


CALayer *sublayer = [CALayer layer];
sublayer.backgroundColor = [UIColor blueColor].CGColor;
sublayer.shadowOffset = CGSizeMake(5, 8);
sublayer.shadowRadius = 2.0;
sublayer.shadowColor = [UIColor grayColor].CGColor;
sublayer.shadowOpacity = 0.8;
sublayer.frame = imageLayer.frame;
sublayer.borderColor = [UIColor blackColor].CGColor;
sublayer.borderWidth = 0.0;
sublayer.cornerRadius = 10.0;

[imageButton.layer.superlayer insertSublayer:sublayer atIndex:0];

Now How Can I make top corners rounded.


Solution

  • You can have a look at the ColoredRoundedButton open source implementation. It uses the cornerRadius property of the UIButton's CALayer to get rounded corners.