iphone - 无法以编程方式将正确大小的图像添加到 UIBarButtonItem

标签 iphone ios objective-c

我在尝试使用图像而不是 UIBarButtonItem 的标题时遇到了麻烦。我的理解是,为 UIBarButtonItem 设置图像会自动缩放图像以正确调整 UIBarButtonItem 的大小。这是有道理的,因为似乎没有任何方法可以调整图像的大小(您不能设置 imageView,或添加 subview ,或任何东西)。但是,当我使用两个尺寸完全相同 (24x24) 的不同图像时,我得到了不同的结果:

myAddButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"plus.png"] style:UIBarButtonSystemItemAdd target:self action:@selector(addButtonTapped)];
self.navigationItem.rightBarButtonItem = myAddButton;

myLeftButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"gear.png"] style:UIBarButtonSystemItemAdd target:self action:@selector(settingsButtonTapped)];
self.navigationItem.leftBarButtonItem = myLeftButton;

导致以下内容:

enter image description here

同样,gear.png 和 plus.png 大小相同,都是 24x24。我已经在 Photoshop 中确认了这一点。

现在,如果我采用完全相同的代码行,只需将 gear 替换为加号即可:

myAddButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"plus.png"] style:UIBarButtonSystemItemAdd target:self action:@selector(addButtonTapped)];
self.navigationItem.rightBarButtonItem = myAddButton;

myLeftButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"plus.png"] style:UIBarButtonSystemItemAdd target:self action:@selector(settingsButtonTapped)];
self.navigationItem.leftBarButtonItem = myLeftButton;

我明白了:

enter image description here

颜色的差异是由于启用了一个而另一个未启用。

我只想让它们成为 UIButtons,这样我就可以使用 imageViews 并自己手动设置它,除了我喜欢使用 UIBarButtonItem 给我的背景,而且我不想在 Photoshop 中重新创建它。

关于这里发生的事情以及我如何解决它有什么想法吗?

提前致谢。

编辑:

原来这个问题是因为我需要清除我的构建并从模拟器中删除应用程序,因为它是从我之前使用的 48x48 图像中提取的。

最佳答案

确保您的项目中有正确的图像。而且,是的,有时尝试清理您的项目。从设备/模拟器中删除该应用程序也很有帮助。

关于iphone - 无法以编程方式将正确大小的图像添加到 UIBarButtonItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14890801/

相关文章:

iphone - UIView TransitionFromView 示例?

ios - 检测 UIImagePicker OverlayView 方向

ios - 在前台使用 NSURLConnection 启动的连接如何在后台继续?

objective-c - 使用 Finder + ScriptingBridge 按路径名/URI 处理文件

iphone - Objective C 中 Bool 的值

iphone - 如何指定仅与 iPhone 兼容 - 而不是 iPad 或 iPod?

ios - 继承初始化类中的Swift错误

ios - Swift 如何在堆栈 View 中调整图像大小

iphone - 在SQLite中插入数据时遇到问题?

ios - 如何在设备屏幕因不活动而关闭时/之前立即执行方法?