Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton ignoring contentMode when highlighted (adjustsImageWhenHighlighted)

I set a UIImage for my UIButton using [myButton setImage:forState:]; and I set it's contentMode using [[myButton imageView] setContentMode:UIViewContentModeScaleAspectFit]; But when you tap the button, it goes back to UIViewContentModeScaleToFill and stretches my image out.

using adjustsImageWhenHighlighted fixes this, but then I loose the darkening effect, which I would like to keep.

Any suggestions on how to cope with this?

like image 597
Kenny Winker Avatar asked Dec 12 '10 02:12

Kenny Winker


2 Answers

UIButton *imageBtn = [UIButton ...
imageBtn.adjustsImageWhenHighlighted = NO;

[imageBtn addTarget:self action:@selector(doSomething:) forControlEvents:UIControlEventTouchUpInside];

[imageBtn addTarget:self action:@selector(doHighlighted:) forControlEvents:UIControlEventTouchDown];
[imageBtn addTarget:self action:@selector(doHighlighted:) forControlEvents:UIControlEventTouchDragEnter];
    [imageBtn addTarget:self action:@selector(doCancelHighlighted:) forControlEvents:UIControlEventTouchDragExit];

-(void)doSomething:(UIButton *)button{
    ...
    [self performSelector:@selector(doCancelHighlighted:) withObject:button afterDelay:0.2f];
}

-(void)doHighlighted:(UIButton *)button{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 300, 300)];
    imageView.backgroundColor = [UIColor blackColor];
    imageView.alpha = 0.7;
    imageView.tag = 1000;
    [button addSubview:imageView];
}

-(void)doCancelHighlighted:(UIButton *)button{
    UIView *view = [button subviewWithTag:1000];
    [UIView animateWithDuration:0.2f animations:^{
        view.alpha = 0;
    } completion:^(BOOL finished) {
        [view removeFromSuperview];        
    }];
}
like image 82
TonnyTao Avatar answered Oct 01 '22 17:10

TonnyTao


my solution to this problem (maybe not efficient but it gives you an oportunity to customize the highlight effect, i think it looks better then standard one) is:

  • subclass UIButton of course.
  • add property @property (nonatomic, retain) UIView *highlightView;
  • method for setting image (my method, you can use different mode important to set adjustsImageWhenHighlighted property)

    [self setImage:image forState:UIControlStateNormal];
    [self setAdjustsImageWhenHighlighted:NO];
    
  • override setHighlighted: method like so:

    \- (void)setHighlighted:(BOOL)highlighted {
        if (!highlightView) {
            self.highlightView = [[[UIView alloc] initWithFrame:self.bounds] autorelease];
            self.highlightView.backgroundColor = [UIColor darkGrayColor];
            self.highlightView.alpha = 0.0;
            [self addSubview:self.highlightView];
        }
        if (highlighted) {
            [UIView beginAnimations:@"highlight" context:nil];
            [UIView setAnimationDuration:0.2];
            highlightView.alpha = 0.5;
            [UIView commitAnimations];
        }
        else {
            [UIView beginAnimations:@"highlight" context:nil];
            [UIView setAnimationDuration:0.2];
            highlightView.alpha = 0.0;
            [UIView commitAnimations];
        }
    }
    

This works for me, but if there is a better way, i'll be glad to get to know it.

like image 39
bartekhugo Avatar answered Oct 01 '22 16:10

bartekhugo