我确信我在做一些愚蠢的事情。我在UIButton之上构建了一个类别,我希望它能够获取分配给它的所有背景图像(不同的状态)并将它们转换为可伸缩版本并将它们重新应用回按钮。
- (void)enableBackgroundImageStrechingWithLeftCapWidth:(float)leftCapWidth withTopCapHeight:(float)topCapHeight;
{
UIImage *backgroundimageNormal = [self backgroundImageForState:UIControlStateNormal];
if (backgroundimageNormal != nil)
{
UIImage *stretchImage = [backgroundimageNormal stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];
[self setBackgroundImage:stretchImage forState:UIControlStateNormal];
}
UIImage *backgroundimageSelected = [self backgroundImageForState:UIControlStateSelected];
if (backgroundimageSelected != nil)
{
UIImage *stretchImage = [backgroundimageSelected stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];
[self setBackgroundImage:stretchImage forState:UIControlStateSelected];
}
UIImage *backgroundimageHighlighted = [self backgroundImageForState:UIControlStateHighlighted];
if (backgroundimageHighlighted != nil)
{
UIImage *stretchImage = [backgroundimageHighlighted stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];
[self setBackgroundImage:stretchImage forState:UIControlStateHighlighted];
}
UIImage *backgroundimageDisabled = [self backgroundImageForState:UIControlStateDisabled];
if (backgroundimageDisabled != nil)
{
UIImage *stretchImage = [backgroundimageDisabled stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];
[self setBackgroundImage:stretchImage forState:UIControlStateDisabled];
}
}
似乎工作,但按钮现在无法点击
答案 0 :(得分:0)
似乎突出显示的状态会导致问题!
我删除了突出显示的块,它运行正常吗?
答案 1 :(得分:0)
如果您没有突出显示/禁用状态的自定义图像,但依赖UIButton应用突出显示/禁用效果,则[self backgroundImageForState: UIControlStateHighlighted]
将不会返回nil
。它将返回一个指向正常状态图像的指针。
使用您的代码,您可以有效地为所有状态设置相同的图像。但是,它们看起来像是框架的自定义图像。这会禁用内置高亮/禁用效果。