我正在使用以下代码在工具栏上添加带有图像的按钮,但是它只显示颜色,没有图像显示。

 UIBarButtonItem *locationItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"butImg.png"] style:UIBarButtonItemStylePlain target:self action:@selector(butImgPressed:)];

任何帮助将不胜感激。

最佳答案

唯一的方法:

UIButton *btnName = [UIButton buttonWithType:UIButtonTypeCustom];

[btnName setFrame:CGRectMake(0, 0, 23, 21)];

[btnName setBackgroundImage:[UIImage imageNamed:@"butImg.png"] forState:UIControlStateNormal];

[btnName addTarget:self action:@selector(butImgPressed:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *locationItem = [[UIBarButtonItem alloc] initWithCustomView:btnName];

关于objective-c - UIToolBar按钮图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9871265/

10-09 02:21