UIButton ignoring contentMode when highlighted (adjustsImageWhenHighlighted)
Asked Answered
H

3

12

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?

Huckaby answered 12/12, 2010 at 2:44 Comment(1)
i have the same problem. starting to think there's no way around this...Disarticulate
L
4
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];        
    }];
}
Loaves answered 12/10, 2011 at 10:16 Comment(1)
2 problems: 1. you never release the imageview in doHighlighted:, 2. subviewWithTag: should be viewWithTag:. Also a tip: instead of CGRectMake() in doHighlighted:, just use button.bounds; Other than that, this works fine.Correggio
H
2

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.

Hoahoactzin answered 4/4, 2011 at 11:53 Comment(0)
H
0

For some reason that happens when you set the content mode after setting the image for any state.

Make sure you set the content mode before setting the images, programatically and on InterfaceBuilder as well. To fix this on IB make sure you remove all images set for all states, set the content mode, and then put the images back again.

That fixed it for me.

Handknit answered 27/2, 2013 at 11:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.