How to round the corners of a button

I have a rectangle image (jpg) and want to use it to fill the background of a button with rounded corner in xcode.

I wrote the following:

UIButton *button = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
CGRect frame = CGRectMake(x, y, cardWidth, cardHeight);
button.frame = frame;
[button setBackgroundImage:backImage forState:UIControlStateNormal];

However, the button I get with that approach doesn't have its corners rounded: it is instead a plain rectangle that looks exactly like my original image. How can I get instead an image with rounded corner to represent my button?

Thanks!


I think I got your problem, I tried the following solution with the UITextArea and I suppose this will work with UIButton as well.

first of all import this in your .m file -

#import <QuartzCore/QuartzCore.h>

and then in your loadView method add following lines

    yourButton.layer.cornerRadius = 10; // this value vary as per your desire
    yourButton.clipsToBounds = YES;

Hope this works for you and if yes do communicate.


You can achieve by this RunTime Attributes

在这里输入图像描述

we can make custom button.just see screenshot attached.

kindly pay attention :

in runtime attributes to change color of border follow this instruction

  • create category class of CALayer

  • in h file

    @property(nonatomic, assign) UIColor* borderIBColor;
    
  • in m file:

    -(void)setBorderIBColor:(UIColor*)color {
        self.borderColor = color.CGColor;
    }
    
    -(UIColor*)borderIBColor {
        return [UIColor colorWithCGColor:self.borderColor];
    }
    
  • now onwards to set border color check screenshot

    更新的答案

    thanks


    You may want to check out my library called DCKit. It's written on the latest version of Swift .

    You'd be able to make a rounded corner button/text field from the Interface builder directly:

    DCKit:圆角按钮

    It also has many other cool features, such as text fields with validation, controls with borders, dashed borders, circle and hairline views etc.

    链接地址: http://www.djcxy.com/p/31416.html

    上一篇: 如何使按钮的边角变圆?

    下一篇: 如何圆角按钮