Ios UIButton-仅在一个方向上可拉伸的背景图像?
我有一个UIButton对象,其中我使用一个可拉伸的图像作为背景,因此我可以始终具有灵活的按钮大小 问题是我希望图像有一个固定的高度(比如32px),但我希望有一个更高的可触摸区域(苹果用户界面指南总是说至少44px高) 如果我在x中有一个可拉伸的图像,不幸的是它也在y中拉伸。我想告诉图像不要在y方向拉伸。这可能吗Ios UIButton-仅在一个方向上可拉伸的背景图像?,ios,uibutton,uiimage,custom-controls,Ios,Uibutton,Uiimage,Custom Controls,我有一个UIButton对象,其中我使用一个可拉伸的图像作为背景,因此我可以始终具有灵活的按钮大小 问题是我希望图像有一个固定的高度(比如32px),但我希望有一个更高的可触摸区域(苹果用户界面指南总是说至少44px高) 如果我在x中有一个可拉伸的图像,不幸的是它也在y中拉伸。我想告诉图像不要在y方向拉伸。这可能吗 [编辑]是的。回答我自己的问题:所以,为了帮助别人,我可以回答我自己的问题: @interface StretchableXButton : UIButton { CGFlo
[编辑]是的。回答我自己的问题:所以,为了帮助别人,我可以回答我自己的问题:
@interface StretchableXButton : UIButton
{
CGFloat imageHeight;
}
@property CGFloat imageHeight; // we need this later when we override an instance method
+ (id)buttonWithFrame:(CGRect)frame;
@end
现在实施:
@implementation StretchableXButton
@synthesize imageHeight;
+ (id)buttonWithFrame:(CGRect)frame
{
StretchableXButton *button = [super buttonWithType:UIButtonTypeCustom];
UIImage *normalImage = [UIImage imageNamed: @"ButtonBGNormal.png" ];
UIImage *highlightedImage = [UIImage imageNamed:@"ButtonBGHighlighted.png" ];
button.frame = frame;
button.imageHeight = normalImage.size.height; // we need him later in the method below
// make the images stretchable
normalImage = [normalImage stretchableImageWithLeftCapWidth:normalImage.size.width/2 topCapHeight:normalImage.size.height/2];
highlightedImage = [highlightedImage stretchableImageWithLeftCapWidth:normalImage.size.width/2 topCapHeight:normalImage.size.height/2];
button.backgroundColor = [UIColor clearColor];
// SET OTHER BUTTON PROPERTIES HERE (textLabel, fonts, etc.)
[button setBackgroundImage:normalImage forState:UIControlStateNormal];
[button setBackgroundImage:highlightedImage forState:UIControlStateHighlighted];
return button;
}
// THIS IS THE TRICK. We make the height of the background rect match the image.
-(CGRect)backgroundRectForBounds:(CGRect)bounds
{
CGRect bgRect = bounds;
bgRect.origin.y = (bounds.size.height - imageHeight)/2.0f;
bgRect.size.height = imageHeight;
return bgRect;
}
@end
因此,为了帮助他人,我可以回答自己的问题:
@interface StretchableXButton : UIButton
{
CGFloat imageHeight;
}
@property CGFloat imageHeight; // we need this later when we override an instance method
+ (id)buttonWithFrame:(CGRect)frame;
@end
现在实施:
@implementation StretchableXButton
@synthesize imageHeight;
+ (id)buttonWithFrame:(CGRect)frame
{
StretchableXButton *button = [super buttonWithType:UIButtonTypeCustom];
UIImage *normalImage = [UIImage imageNamed: @"ButtonBGNormal.png" ];
UIImage *highlightedImage = [UIImage imageNamed:@"ButtonBGHighlighted.png" ];
button.frame = frame;
button.imageHeight = normalImage.size.height; // we need him later in the method below
// make the images stretchable
normalImage = [normalImage stretchableImageWithLeftCapWidth:normalImage.size.width/2 topCapHeight:normalImage.size.height/2];
highlightedImage = [highlightedImage stretchableImageWithLeftCapWidth:normalImage.size.width/2 topCapHeight:normalImage.size.height/2];
button.backgroundColor = [UIColor clearColor];
// SET OTHER BUTTON PROPERTIES HERE (textLabel, fonts, etc.)
[button setBackgroundImage:normalImage forState:UIControlStateNormal];
[button setBackgroundImage:highlightedImage forState:UIControlStateHighlighted];
return button;
}
// THIS IS THE TRICK. We make the height of the background rect match the image.
-(CGRect)backgroundRectForBounds:(CGRect)bounds
{
CGRect bgRect = bounds;
bgRect.origin.y = (bounds.size.height - imageHeight)/2.0f;
bgRect.size.height = imageHeight;
return bgRect;
}
@end
以答案的形式回答自己的问题,而不是对问题进行编辑。你甚至可以这样接受。把你自己的问题作为答案回答,而不是对问题的编辑。你甚至可以这样接受。