我想将图像设置为按钮的背景。所以我创建了一个图像:
然后我想将它实现为可拉伸(stretch)的:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 200, 400)];
[button setTitle:@"Test Button" forState:UIControlStateNormal];
UIImage *buttonImage = [_model.background resizableImageWithCapInsets:UIEdgeInsetsMake(6, 8, 6, 7)]; //this is screen 1
//UIImage *buttonImage = _model.background; //this is screen 2
[button addTarget:self action:@selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[self addSubview:button];
我想像在屏幕 2 中一样进行背景拉伸(stretch),但角落会像屏幕 1 一样锐利。_model.background 是 UIImage 包含来自这篇文章顶部的原始按钮背景。
我用对了吗?我遵循文档,所以
UIEdgeInsetsMake(top,left,bottom,right)
应该是无法触及的角落吧?
最佳答案
根据文档,插图内的部分(未通过帽设置锁定的部分)是平铺的。所以你应该有一个小的 1 像素区域来平铺。例如。使插图为 UIEdgeIndetsMake(20,8,19,7)
这会产生:
关于objective-c - 带有 resizableImage 选项的 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11616403/