18

I want to create Custom UIButton with rectangular shape. for this I am using rectangular view as a Background for UIButton and making UIbuttons background colour as clearcolor. But border of UIbutton still remains. any suggestions on how to make border disappear? Thanks for any help in advance..

3

5 Answers 5

41

Try this:

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button.layer setBorderColor:[[UIColor clearColor] CGColor]];

but don't forget to add in your .h or .m file

#import <QuartzCore/QuartzCore.h>
1
  • 3
    you need to add the border's width also : [button.layer setBorderWidth:1.0f];
    – youssman
    Jul 2, 2016 at 21:17
13

I wasted a few minutes and got frustrated that the border was not showing up even though I was setting the layer property as suggested above.

When I set the width using the following, I saw the button border.

[[button layer] setBorderWidth:2.0f];
5
  button.layer.borderColor = UIColor.orangeColor().CGColor

swift example for orange border

2

You can set the border properties on the CALayer by accessing the layer property of the button.

Add Quartz

#import <QuartzCore/QuartzCore.h>

Set properties of the button:

[[myButton layer] setBorderWidth:2.0f];
[[myButton layer] setBorderColor:[UIColor AnyColor].CGColor];
0

You should set type of your UIButton to Custom, not Rounded Rect (if you are creating button via IB).

If you are creating UIButton programmatically then you this code:

UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];

And after that you can make your own customizations.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.