UIButton忽略内容模式突出显示(adjustImageWhenHighlighted)

我使用[myButton set Image:forState:]为UIButton设置了一个UIImage;
我设置它的contentMode使用[[myButton imageView] setContentMode:UIViewContentModeScaleAspectFit];
但是当您点击按钮时,它将返回到UIViewContentModeScaletoFill并将我的图像拉出.

使用adjustimageWhenHighlighted修复这个,但是然后我放宽了变暗的效果,我想保留.

有什么建议如何应付这个?

解决方法

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

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...