iOS:如何用图像覆盖整个按钮

时间:2013-11-02 11:42:12

标签: ios objective-c ipad

我目前正在开发iOS应用程序,我不断更改两个按钮的图片。问题是不断变化的按钮图片不会拉伸整个按钮。我想知道这样做的最佳方法是什么。 我相信我正在设置我的按钮图像,我无法弄清楚该怎么做。任何建议将不胜感激。

leftButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",      [orangeImageArray objectAtIndex:randomPicture2]]] forState:UIControlStateNormal];

3 个答案:

答案 0 :(得分:0)

试试这个:

 UIImage *btnImage = [UIImage imageNamed:@"image.png"];
 [leftButton setImage:btnImage forState:UIControlStateNormal];

答案 1 :(得分:0)

此代码可让您自定义按钮:

UIImage *img = [UIImage imageNamed:@"icon.png"];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.bounds = CGRectMake(0, 0, img.size.width, img.size.height);
[btn setBackgroundImage:img forState:UIControlStateNormal];
[btn addTarget:self action:@selector(prefer) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.rightBarButtonItem = barButton;

答案 2 :(得分:0)

您的按钮类型是UIButtonTypeCustom吗?

示例:

 UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
 [leftButton setImage:btnImage forState:UIControlStateNormal];