Making a custom Button using a UIView or overriding UIButton?
I need to generate a custom button through code, this is how i am currently doing it.
-(void) initialiseButtons
{
int ypos = playerImage.frame.origin.y + playerImage.frame.size.height + 8;
for(int i=0; i<totalButtons; i++)
{
UIButton *newButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[newButton setFrame:CGRectMake(20, ypos, 220, 30)];
newButton.tag = 10 + i;
[newButton addTarget:self action:@selector(statTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.frontView addSubview:newButton];
ypos += 30 + 7;
}
}
This creates the blank buttons perfectly through code, gives them a tag and assigns an callback function on touchUpInside.
The custom button needs to be able to handle showing an image when pressed down. It needs to be able to draw 2 pieces of Text. 1 aligned to the left hand side of the button and 1 aligned to the righthand side of the button.
My boss suggested instead of buttons I use a View. I dont understand how this will work. When i start thinking about it, i think it would require having a viewcontroller dedicated to the buttons. And some draw method? It sounds complicated and I am not grasping how it can be done.
Is there a simpler method by making a custom class overriding UIButton? I made a simple test class earlier but nothing was drawn in the buttons locations when I used them in place of the Normal UIButton class. I expected i would see the buttonUp.png drawn.
#import <Foundation/Foundation.h>
#import <UIKit/UIKit开发者_StackOverflow.h>
@interface CardButton : UIButton {
}
@end
#import "CardButton.h"
#import <UIKit/UIKit.h>
@implementation CardButton
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
[self setImage:[UIImage imageNamed:@"buttonUp.png"] forStates:UIControlStateNormal];
self.adjustsImageWhenHighlighted = YES;
}
return self;
}
@end
Anybody able to give me some pointers on this? I'm pretty blocked at the moment. Have been reading some various other threads related to buttons but nothing that made it clear in my head how to tackle the problem
Personally, I would subclass UIButton. Despite all the talk that you can't subclass UIButton, Apple even talks about subclassing UIButton in the UIButton documentation.
In the subclass I would create a UIView with the two labels (or images or whatever) and add them as subviews to the button (be sure to set interactive for the text and view as FALSE).
What is awesome about this is that it leverages the UIButton code and keeps you from reinventing the wheel.
You should not (I'd almost say you can't) subclass UIButton
, as it turns out to be a class cluster, and it would be impractical (read: impossible) to derive from that.
So your two options are:
1) Use standard UIButton
of custom type, add the elements you want to show (i.e. UILabel
) and hook up actions for touch down and touch up and react accordingly (change the views, trigger actions etc.)
2) Use a custom UIView
, implement drawRect:
to draw how you like it or use custom views as subviews as in 1). Then use the touchesBegan:
, touchesEnded:
etc. messages to react accordingly (or UIGestureRecognizer
if you can live with 3.2+ compatibility).
You might build a factory to build those buttons if they all are very similar, so that your code becomes clean and non-repetive.
Edit as per iOS 6: Apple now mentions subclassing in the UIButton docs, so while still not very well defined, it seems quite safe now to do so.
An easy way to do this, is to override a UIView. In the view you add a UIButton as Subview. This way you have a reusable class without the need to re-implement button behaviour.
You can style your button the way you want in the initWithFrame method of your UIView derived class. If you use this class as target for your button events, you can implement special behaviour easily, like showing the image.
For the two pieces of text, you create two labels and add them as subviews to the button.
精彩评论