iOS自定义键盘可拉伸图像停止拉伸

iOS自定义键盘可拉伸图像停止拉伸,ios,uibutton,uiimage,uikeyboard,Ios,Uibutton,Uiimage,Uikeyboard,我为UITextField制作了一个自定义键盘,由18个UIButton组成。它是一个UIView,设置为文本字段的inputView 按钮是在UIView的initWithFrame:方法中使用代码创建的 UIImage* buttonImage =[[UIImage imageNamed:@"keyboard-button-background.png"] stretchableImageWithLeftCapWidth:1.0 topCapHeight:79.0]; UIImage* bu

我为UITextField制作了一个自定义键盘,由18个UIButton组成。它是一个UIView,设置为文本字段的inputView

按钮是在UIView的
initWithFrame:
方法中使用代码创建的

UIImage* buttonImage =[[UIImage imageNamed:@"keyboard-button-background.png"] stretchableImageWithLeftCapWidth:1.0 topCapHeight:79.0];
UIImage* buttonPressedImage =[[UIImage imageNamed:@"keyboard-button-pressed-background.png"] stretchableImageWithLeftCapWidth:1.0 topCapHeight:79.0];

UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:title forState:UIControlStateNormal];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button setBackgroundImage:buttonPressedImage forState:UIControlStateHighlighted];

button.frame = CGRectMake(xPosition, yPosition, buttonWidth, buttonHeight);
按下按钮后,该按钮将设置为
已启用=否
。至少有一个按钮处于启用状态时,背景会完全拉伸。如果所有按钮都被禁用,并且UITextfield
退出FirstResponder
,然后UITextfield再次成为第一响应者,则按钮会出现,但背景图像似乎不再拉伸(至少不会在垂直方向上,水平方向看起来很好)

我真的不确定从哪里开始寻找解决方案。我猜这就是自定义键盘绘制子视图的方式,但我不确定

是否可以通过在
layoutSubviews
drawRect:
或类似内容中设置背景图像来解决此问题?水平拉伸效果很好这一事实让我怀疑UIButton的框架在绘制时是否不正确

编辑 最左边的单元格只是标签,右边带有数字的单元格是UITextfields,具有自定义键盘

键盘的外观:

键盘的实际外观:第3单元的第一个响应者辞职,然后给出1个第一个响应者(禁用所有键),然后给出4个第一个响应者:
您正在使用的背景图像的大小是多少

无论如何,自iOS 5以来,不再使用带有LeftCapWidth:topCapHeight:的StretcableImageWithLeftCapWidth:topCapHeight:。
尝试改用
resizebleimagewithcapinsets:

为了处理不同的iOS API,我为它们做了一个分类 UIImage

@implementation UIImage (SPResizableImage)

- (UIImage *)spResizableImageWithCapInsets:(UIEdgeInsets)edge
{
    UIImage * resizableImage = nil;

    if ([self respondsToSelector:@selector(resizableImageWithCapInsets:)]) // iOS 5
    {
        resizableImage = [self resizableImageWithCapInsets:edge];
    }
    else
    {
        resizableImage = [self stretchableImageWithLeftCapWidth:edge.left
                                                   topCapHeight:edge.top];
    }

    return resizableImage;
}

@end

你能包括一些截图吗?很难想象这应该是什么样子。你在哪里设置你的矩形?我已经添加了一行,我在
initWithFrame:
中设置了按钮的框架。图像大小是
9 x 80
,这个特定键盘上的按钮是
53 x 65
。我有几个不同的键盘布局,但它们使用相同的图像。有趣的是,水平拉伸有效,但垂直拉伸似乎不起作用。哦,我必须支持iOS 4,所以我需要使用
拉伸图像…
如果可能的话,我认为问题出在上限值上。我有一个类似的问题,拉伸图像。嗯,这似乎是工作!奇怪的是,
StretcableImageWithLeftCapWidth:topCapHeight:
在某些情况下不太管用,但我们开始了!谢谢你的帮助。