Search code examples
iosobjective-c

How to create border in UIButton?


I use custom button in my app named "addButton" and I want to border it with white color how can i get the white color border around my custom button?


Solution

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

    First, add Quartz

    #import <QuartzCore/QuartzCore.h>
    

    Set properties:

    myButton.layer.borderWidth = 2.0f;
    myButton.layer.borderColor = [UIColor greenColor].CGColor;
    

    See:

    https://developer.apple.com/documentation/quartzcore/calayer#//apple_ref/occ/cl/CALayer

    The CALayer in the link above allows you to set other properties like corner radius, maskToBounds etc...

    Also, a good article on button fun:

    https://web.archive.org/web/20161221132308/http://www.apptite.be/tutorial_custom_uibuttons.php