2010-12-12 89 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上解決這個問題,請確保刪除爲所有狀態設置的所有圖像,設置內容模式,然後再次放回圖像。

已經爲我修好了。