12

UIImage我使用 [ 为我的 UIButton设置了一个,myButton setImage:forState:]; 我设置它正在contentMode使用[[myButton imageView] setContentMode:UIViewContentModeScaleAspectFit]; 但是当你点击按钮时,它会返回UIViewContentModeScaleToFill并拉伸我的图像。

使用adjustsImageWhenHighlighted解决了这个问题,但后来我失去了我想保留的变暗效果。

有关如何应对此问题的任何建议?

4

3 回答 3

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];        
    }];
}
于 2011-10-12T10:16:27.320 回答
2

我对这个问题的解决方案(可能效率不高,但它为您提供了自定义突出显示效果的机会,我认为它看起来比标准效果更好)是:

  • 当然是 UIButton 的子类。
  • 添加属性@property (nonatomic, retain) UIView *highlightView;
  • 设置图片的方法(我的方法,你可以使用不同的重要模式来设置adjustsImageWhenHighlighted属性)

    [self setImage:image forState:UIControlStateNormal];
    [self setAdjustsImageWhenHighlighted:NO];
    
  • 像这样覆盖 setHighlighted: 方法:

    \- (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];
        }
    }
    

这对我有用,但如果有更好的方法,我会很高兴了解它。

于 2011-04-04T11:53:13.163 回答
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.

于 2013-02-27T11:45:26.207 回答