UIBarButtonItem 在 iPhone 而非 iPad 上显示图像

UIBarButtonItem displaying image on iPhone but not iPad

我看到一些关于图像在设备和模拟器之间显示不同的问题。这是不是。该图像显示在 iPhone6 设备上,但不显示在 iPad Air 设备上。如果我从图像更改为简单文本,按钮会显示在两者上。

我这样添加按钮:

UIBarButtonItem searchButton = 
                      new UIBarButtonItem(
                                         UIImage.FromFile("Search-22"),           
                                            UIBarButtonItemStyle.Plain, null);

this.NavigationItem.SetRightBarButtonItem(searchButton, false);

我怎样才能让它在两者上显示图像?

我也遇到过这种情况。

首先你需要扩展,平台之间存在一些细微差别 iPad / iPhone。

个人调试,我会分解它:

var searchImage = UIImage.FromFile("Search-22.png");
UIBarButtonItem searchButton = new UIBarButtonItem(searchImage, UIBarButtonItemStyle.Plain, null);

this.NavigationItem.SetRightBarButtonItem(searchButton, false);

然后你可以调试每一行,并检查searchImage对象是否为空。

同样明显,请检查您的命名约定:

  • MyImage@2x.png [通用]
  • MyImage@2x~iphone.png [iPhone 仅]
  • MyImage@2x~ipad.png [iPad 仅]

问题出在对 FromFile 而不是 FromBundle 的调用中。一旦我切换到 FromBundle,我就可以在我的按钮中使用查看图像。

UIImage searchImage = UIImage.FromBundle("Search-22.png");UIImage searchImage = UIImage.FromBundle("Search-22.png");

我从这个 SO 问答中得到了最终解决方案,MonoTouch UIImage.FromFile not loading retina assets

iPad Mini 上的图像确实看起来有点参差不齐,所以我可能还有其他问题