Search code examples
swiftxcode6cgrect

How to change the shape of a UIButton in Swift?


I am trying to make an arrow shape. I understand that it will be done with CGRect method but can you give a complete example of how it will be done in SWIFT?


Solution

  • This can be done programmatically by first subclassing UIButton and overriding drawRect within it to form the shape of an arrow via bezier paths. Then simply set the title of the button to be whatever you want your text to be and position it accordingly within the shape you have drawn.

    The benefit of doing it this way is that you can easily change the color of, scale and dimensions of the arrow since you control how the object is actually being drawn.

    A good tool for drawing complex bezier paths is paintCode. I'm using the trial version and can pretty easily make shapes like this now.