Alexandr Kurilin
Alexandr Kurilin

Reputation: 7845

Any point in subclassing UIButton to create a custom looking button?

I want to create a set of buttons that would work and feel very much like the Metro-style tiles that are available on Windows Phones. I would like to allow the user to tap them to access their underlying functionality (open a modal or something or that sort).

  1. I'm concerned that subclassing UIButton will not get me there. I need square corners and dynamic content inside the tiles themselves. The posts I've found around SO seem to suggest that subclassing a UIButton would not be a good idea for several reasons and that I should instead use UIView. That reply is from 2010, and I have no idea if in the 2+ years since then there have been considerable changes to how one would achieve that effect. Most users these days will have iOS 5.1+ if not 6.0 soon.

  2. Let's say I do go the UIView route. Should I implement UIResponder's touch events or should I instead go the UITapGestureRecognizer route? What's the better practice in 2012?

Thank you!

Upvotes: 1

Views: 680

Answers (3)

zavié
zavié

Reputation: 4321

A use case would be to use a custom-looking button within Storyboard / Interface Builder. Having a subclass allows you to directly use this custom-looking button from Interface Builder.

Upvotes: 0

Anurag
Anurag

Reputation: 141889

I'd suggest subclassing from UIControl instead of UIButton. I've seen how those windows tiles look and work in some videos including flipping animations when content changes etc., and it would be easier to package that functionality into a Tile class or something similar than having to carry a modified UIButton everywhere, and doing it for every such instance you need to create.

The reason I'm suggesting to not subclass from UIButton is because it provides support for 1 image and 1 label. You seem to have multiple images and labels on all tiles, so you'll have to start managing your own subviews. Instead of working around the 1 label and image you get in a UIButton, it's cleaner to start from scratch with a UIControl and create your tiles.

Upvotes: 2

CocoaEv
CocoaEv

Reputation: 2984

if you just desire to have square metro style buttons that pretty much act like regular buttons (push them and something happens), then you can get all the functionality out of the standard UIButton class using a custom button without subclassing.

here is a quick example, in code, but this can be accomplished in IB as well. say you have an image called: tile1.png

//set the image
UIImage *image = [UIImage imageNamed:@"tile1.jpg"];

//create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

//add the image
[button setImage:image forState:UIControlStateNormal];

//define the action
[button addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];

If you need to modify the frame size you can via button.frame You can also modify a number of other button properties.

Did you know all that and were looking for something else or does that help? be well

Upvotes: 0

Related Questions