sergiocg90
sergiocg90

Reputation: 499

UIButton ControlState not responding as wanted

Well, I have a custom uitabbarcontroller with 3 UIButtons on it (simulating tabs). I have set background images for normal, selected and highlighted states (selected and highlighted are both the same). Everything works fine but one thing: When I have on tab (button) selected, and that tab gets pressed again, instead of highlighting it shows the button being pressed (getting darker). I have tried setting to NO the property adjustsImageWhenHighlighted, but instead of getting darker, it shows the Normal state background.

Any suggestion?

EDIT: This is the code I have in the UITabBarController subclass

#import "MyTabBarViewController.h"

@interface MyTabBarViewController ()

@end

@implementation MyTabBarViewController

ExploreViewController *exploreController;
ProfileViewController *profileController;
UIButton* leftButton;
UIButton* rightButton;

- (void)viewDidLoad
{
    [super viewDidLoad];

    exploreController = [[ExploreViewController alloc] initWithNibName:@"ExploreViewController" bundle:nil];

    profileController = [[ProfileViewController alloc] initWithNibName:@"ProfileViewController" bundle:nil];

    self.viewControllers = [NSArray arrayWithObjects:exploreController, profileController, nil];

    [self addLeftButtonWithImage: [UIImage imageNamed:@"LeftTabBarIcon"] highlightImage:[UIImage imageNamed:@"LeftTabBarIcon_On"]];
    [self addRightButtonWithImage: [UIImage imageNamed:@"RightTabBarIcon"] highlightImage:[UIImage imageNamed:@"RightTabBarIcon_On"]];
}

- (void) leftTabPressed
{
    leftButton.selected = YES;
    rightButton.selected = NO;
    [self setSelectedViewController:exploreController]; 
}

- (void) rightTabPressed
{
    rightButton.selected = YES;
    leftButton.selected = NO;
    [self setSelectedViewController:profileController];    
}

-(void) addLeftButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
{
    leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.adjustsImageWhenHighlighted = NO;
    [[leftButton imageView] setContentMode: UIViewContentModeScaleAspectFit];
    [leftButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [leftButton setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
    [leftButton setBackgroundImage:highlightImage forState:UIControlStateSelected];
    leftButton.frame = CGRectMake(0.0, 367, 160.0, 49.0);
    [leftButton addTarget:self action:@selector(leftTabSelectPressed) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftButton];
}

-(void) addRightButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
{
    rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.adjustsImageWhenHighlighted = NO;
    [[rightButton imageView] setContentMode: UIViewContentModeScaleAspectFit];
    [rightButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [rightButton setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
    [rightButton setBackgroundImage:highlightImage forState:UIControlStateSelected];
    rightButton.frame = CGRectMake(160.0, 367, 160.0, 49.0);
    [rightButton addTarget:self action:@selector(rightTabPressed) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:rightButton];
}

@end

Upvotes: 0

Views: 1032

Answers (2)

Kudit
Kudit

Reputation: 4379

Trick seemed to be changing UIControlStateHighlighted to UIControlStateHighlighted | UIControlStateSelected. Seems odd, but it works.

Upvotes: 0

Malek_Jundi
Malek_Jundi

Reputation: 6160

For your Comment your code will be like this:

-(void) addRightButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
{
    rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.adjustsImageWhenHighlighted = NO;
    rightButton.showsTouchWhenHighlighted = NO;
    [[rightButton imageView] setContentMode: UIViewContentModeScaleAspectFit];
    [rightButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [rightButton setBackgroundImage:highlightImage forState:UIControlStateHighlighted | UIControlStateSelected];
    [rightButton setBackgroundImage:highlightImage forState:UIControlStateSelected];
    rightButton.frame = CGRectMake(160.0, 367, 160.0, 49.0);
    [rightButton addTarget:self action:@selector(rightTabPressed) forControlEvents:UIControlEventTouchUpInside];

    UILongPressGestureRecognizer *longGesture = [[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longGestureDetected:)]autorelease];
    longGesture.delaysTouchesBegan = YES;
    [rightButton addGestureRecognizer:longGesture];

    [self.view addSubview:rightButton];
}

- (void)longGestureDetected:(UILongPressGestureRecognizer*)longGesture
{
    if(longGesture.state == UIGestureRecognizerStateBegan)
        [self rightTabPressed];
}

Upvotes: 2

Related Questions