UIButton 在突出显示时忽略 contentMode(调整ImageWhenHighlighted)

发布于 2024-10-07 04:47:33 字数 357 浏览 7 评论 0原文

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

使用 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮光沉寂 2024-10-14 04:47:33
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];        
    }];
}
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];        
    }];
}
铁轨上的流浪者 2024-10-14 04:47:33

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

  • 当然是子类 UIButton 。
  • 添加属性@property(非原子,保留)UIView *highlightView;
  • 设置图像的方法(我的方法,您可以使用不同的模式重要来设置 adjustmentImageWhenHighlighted 属性)

    [self setImage:image forState:UIControlStateNormal];
    [自我设置AdjustsImageWhenHighlighted:NO];
    
  • 重写 setHighlighted: 方法,如下所示:

    \- (void)setHighlighted:(BOOL)highlighted {
        如果(!highlightView){
            self.highlightView = [[[UIView alloc] initWithFrame:self.bounds] autorelease];
            self.highlightView.backgroundColor = [UIColor darkGrayColor];
            self.highlightView.alpha = 0.0;
            [self addSubview:self.highlightView];
        }
        如果(突出显示){
            [UIView beginAnimations:@"highlight" context:nil];
            [UIView setAnimationDuration:0.2];
            突出显示.alpha = 0.5;
            [UIView提交动画];
        }
        别的 {
            [UIView beginAnimations:@"highlight" context:nil];
            [UIView setAnimationDuration:0.2];
            突出显示.alpha = 0.0;
            [UIView提交动画];
        }
    }
    

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

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.

玩套路吗 2024-10-14 04:47:33

由于某种原因,当您为任何状态设置图像后设置内容模式时,会发生这种情况。

确保设置图像之前以编程方式以及在 InterfaceBuilder 上设置内容模式。要在 IB 上修复此问题,请确保删除为所有状态设置的所有图像,设置内容模式,然后再次放回图像。

这为我解决了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文