Michael
Michael

Reputation: 392

Best way to create Custom (Octagonal) UIButton in Swift

I am about to customize all of my UIButtons by subclassing them into a special UIButton class and I want to have the "look and feel" like below octagonal or some hexagonal buttons. What would be the best way to create such shape UI buttons? So far, I only know how to create rectangle, circular and rounded-edge buttons, and I could not find SO answers or cocoapods for below "look and feel".

  1. Is it possible to create a custom class of such UIButton programmatically?

  2. Or do I need to import an image onto a button to realize below "look and feel"?

  3. Also, could there be any possibility be that App Store does not allow such custom shape buttons?

enter image description here

Upvotes: 0

Views: 313

Answers (1)

jimBeaux27
jimBeaux27

Reputation: 107

  1. Yes you can use UIBezierPath to create arbitrary shapes for a variety of UIKit components that includes UIButton. See tutorial here
  2. I think the above is achievable simply using #1 above in combination with myButton.layer.borderColor/.borderWidth
  3. I am not sure. My guess would be no but I think it would depend on the Apple Human Interface guidelines which seem to be arbitrary in some cases. For what it's worth, I've shipped apps that had very similar buttons but they were elipses and not polygons like you have but still appear very similar.

Update: Forgot about a great example. This is an example of a "bookmark tab" effect I created using a UIButton with a UIBezier path. This has been shipping for years at this point with no issues: enter image description here

Upvotes: 1

Related Questions