最近我遇到了一个问题。我有一个 UIButton 和 2 个图像。1 表示正常状态,1 表示突出显示。问题是正常状态图像是透明的,我应该使用渐变来填充它。但如果我这样做,我突出显示的图像永远不会出现。这是代码的一部分:
UIImage *balloonImage = [[UIImage imageNamed:@"balloon.png"]resizableImageWithCapInsets:UIEdgeInsetsMake(3, 3, 3, 3)];
UIImage *balloonImageDown = [[UIImage imageNamed:@"balloon-down.png"]resizableImageWithCapInsets:UIEdgeInsetsMake(3, 3, 3, 3)];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = CGRectMake(0, 0, 320 - 45 - 10, 100);
gradient.cornerRadius = 3.0f;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:221.0f/255.0f green:231.0f/255.0f blue:246.0f/255.0f alpha:1.0f]CGColor], (id)[[UIColor colorWithRed:191.0f/255.0f green:207.0f/255.0f blue:234.0f/255.0f alpha:1.0f] CGColor], nil];
self.balloonButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.balloonButton.layer insertSublayer:gradient atIndex:0];
[self.balloonButton setBackgroundImage:balloonImage forState:UIControlStateNormal];
[self.balloonButton setBackgroundImage:balloonImageDown forState:UIControlStateHighlighted];
self.theImageView = [[[UIImageView alloc]init]autorelease];
[self.balloonButton addSubview:theImageView];
[self.contentView addSubview:balloonButton];
渐变看起来应该是这样,并且一切正常。如果点击按钮,则更改图像除外。
有没有办法将 balloonImageDown 放在渐变前面?或者我该怎么做?
任何帮助,将不胜感激。