I've created a custom UIButton called SectionButton.
The button have 2 images, one for the Normal State and other for Selected and Highlighted States.
The button also have a text and when the button is pushed down, the TitleEdgesInset must be adjusted.
In the init method I add the methods:
[self addTarget:self action: @selector(buttonHighlighted:) forControlEvents: UIControlStateHighlighted];
[self addTarget:self action:@selector(buttonNormal:) forControlEvents:UIControlStateNormal];
But the method 'buttonNormal:' is never called, so I can't adjust the titleEdgeInsets property.
#import "SectionButton.h"
@implementation SectionButton
- (id)initWithFrame: (CGRect)frame andTitle: (NSString*)title andbaseImageName:(NSString*)imageBaseName
{
if (self = [super initWithFrame: frame])
{
// Create images for button
UIImage* normalImage = [UIImage imageNamed: imageBaseName ];
UIImage* downImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_selected",imageBaseName]];
// Set up button
[self setTitle: [title uppercaseString] forState: UIControlStateNormal]; // Will be used for all states
[self setTitleColor: [UIColor whiteColor] forState: UIControlStateNormal];
[self.titleLabel setFont:[UIFont fontWithName:@"Helvetica" size:12.0]];
[self setBackgroundImage: normalImage forState: UIControlStateNormal];
[self setBackgroundImage: downImage forState: UIControlStateHighlighted];
[self setContentEdgeInsets:UIEdgeInsetsMake(54, 0, 0, 0)];
[self addTarget: self action: @selector(buttonHighlighted:) forControlEvents: UIControlStateHighlighted];
[self addTarget:self action:@selector(buttonNormal:) forControlEvents:UIControlStateNormal];
}
return self;
}
- (void)buttonHighlighted: (id)sender
{
[self setTitleEdgeInsets:UIEdgeInsetsMake(0,0,-16,0)];
NSLog(@"button selected");
}
- (void) buttonNormal: (id)sender {
[self setTitleEdgeInsets:UIEdgeInsetsMake(0,0,0,0)];
NSLog(@"Button normal");
}
}
@end