2010-12-12 81 views
12

我为自己的UIButton一个UIImage使用[myButton setImage:forState:]; ,我把它的contentMode使用[[myButton imageView] setContentMode:UIViewContentModeScaleAspectFit]; 但是,当你轻点按钮,它可以追溯到UIViewContentModeScaleToFill和延伸我的形象了。的UIButton忽略contentMode突出时(adjustsImageWhenHighlighted)

使用adjustsImageWhenHighlighted修复此问题,但后来我松开了我想保留的变暗效果。

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

+0

我有同样的问题。开始认为有没有办法解决这个问题... – jasongregori 2011-07-28 01:17:32

回答

2

我解决这个问题(也许不是有效的,但它给你一个oportunity定制的高光效果,我认为它看起来更好然后一个标准)是:当然

  • 子的UIButton。
  • add property @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]; 
        } 
    } 
    

该作品对我来说,但如果有更好的办法,我会很乐意去了解它。

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

2问题:1.你永远不会在doHighlighted:中释放imageview,2. subviewWithTag:应该是viewWithTag :.还有一个提示:不要使用doHighlighted中的CGRectMake(),只需使用button.bounds;除此之外,这工作正常。 – vakio 2012-01-03 12:53:38

+0

真的很棒的答案,谢谢。 – 2013-06-15 05:28:36

0

出于某种原因,当你设置的内容模式后设置图像的任何国家出现这种情况。

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

已经为我修好了。