0

I have a problem in one of my iOS app projects.

My question is: What is the most efficient way to create a circular UIButton with a specific size that will have an Aspect Fill UIImage, that the user can update anytime from the camera or library?

I'm trying to create a circular UIButton that will have the image that the user uploads. One solution I've tried is to make the image circular and then overlay it with just an empty button, but it doesn't feel too efficient.

I want to use these buttons in a UITableView, so basically more than one of these buttons.

I hope I can get an idea (not necessarily an example code, but more like a logic to make it more efficient and simple) of how to approach this problem with the UIButton so that the image will be updated and Aspect Filled within the properties of the button.

  • I think it would be easier to just add a `UIGestureRecognizer` to recognize taps on a `UIImageView`. Is that an option? If you just need to display a circular image view and have it tappable you don't need all of the logic behind `UIButton`. `UIImageView` has a lot more customization as far as displaying images goes. http://stackoverflow.com/questions/28522104/how-to-make-a-uiimageview-tappable-and-cause-it-to-do-something-swift – Kevin Dec 23 '15 at 01:08
  • I basically need to just display the circular images and make it interactive to the user, so it will recognize the tap. In this way, I can still set and fully customize the properties of the UIImageView and just AspectFill and update the image in it is that right? Thank you I will definitely try this way. – Greg Palkovics Dec 23 '15 at 01:26
  • That's it, I have done that several times and I prefer to avoid `UIButton` and all of its UIControl baggage – Kevin Dec 23 '15 at 01:29
  • "but it doesn't feel too efficient." Don't trust your feelings. You're not a Jedi. If your app has performance problems, use Instruments to figure out where your CPU time or memory is going. (Tip: one button on top of one image will not be a bottleneck.) – rob mayoff Dec 23 '15 at 02:27

2 Answers2

0

I too prefer the UIImageView with UITapGesture. Because by my experience the aspect ratio did not work well in UIButton. UIImageView with layer.cornerRadius with fine to execute from User Interation Side.

S. Karthik
  • 618
  • 5
  • 16
0
  1. you can just add a UIButton in your UITableView and set/change backgroundimage of it with the UIButton click event.
  2. turn the button into a circle.your can user code like this:

    [btn.layer setMasksToBounds:YES]; [btn.layer setCornerRadius:10.0];

    or if you use xib,you can do like this:

    https://i.stack.imgur.com/Ytlqs.jpg

Hai Lu
  • 11
  • 4