積分:@bryanmac他的helpful answer。我用他的代碼作爲出發點,但是發現同樣的事情可以在不使用UIImageView
的情況下實現。
這裏是我的解決方案:
- (UIImage *)translucentImageFromImage:(UIImage *)image withAlpha:(CGFloat)alpha
{
CGRect rect = CGRectZero;
rect.size = image.size;
UIGraphicsBeginImageContext(image.size);
[image drawInRect:rect blendMode:kCGBlendModeScreen alpha:alpha];
UIImage * translucentImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return translucentImage;
}
設置禁用狀態的按鈕的背景圖片:
UIImage * disabledBgImage = [self translucentImageFromImage:originalBgImage withAlpha:0.5f];
[button setBackgroundImage:disabledBgImage forState:UIControlStateDisabled];
編輯:
我進一步完善我的解決方案通過創建與此UIImage
類別方法:
- (UIImage *)translucentImageWithAlpha:(CGFloat)alpha
{
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
CGRect bounds = CGRectMake(0, 0, self.size.width, self.size.height);
[self drawInRect:bounds blendMode:kCGBlendModeScreen alpha:alpha];
UIImage * translucentImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return translucentImage;
}
設置按鈕對禁用狀態的背景圖片:
UIImage * disabledBgImage = [originalBgImage translucentImageWithAlpha:0.5f];
[button setBackgroundImage:disabledBgImage forState:UIControlStateDisabled];
你最終會找到一個解決方案? – bryanmac
我也有興趣找到這個問題的解決方案。 – SAHM
我認爲這個問題的答案是在這裏添加的。 http://stackoverflow.com/questions/3467119/avoiding-making-a-uibutton-transparent-see-through-when-disabling-it –