ios - iphone UIBarButtonItem : setBackgroundImage - Stretched

标签 ios uibarbuttonitem

我们使用 Storyboard添加了 UIBarButtonItem 并且在运行时点击我们想要更改它的图像(功能类似于添加书签/删除书签)。

研究堆栈溢出。很多人建议在 UIBarbuttonItem 中使用 UIButton

下面的代码改变了图像,但它被拉伸(stretch)了(即使是原始图像也显示为拉伸(stretch)。

任何人都可以指导我们仅使用 UIBarButton (不使用 UIButton )更改图像吗?

 [_barButtonAddToFav setBackgroundImage:[UIImage imageNamed:@"ic_add_to_fav.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

最佳答案

UIBarButtonItem 会将整个图像拉伸(stretch)到其条形按钮区域。

要解决这个问题,请在 UIBarButtonItem 中拖动一个 UIButton 并在 UIButton 上设置图像。

将 UIButton 类型设置为自定义,并将所有属性设置为要为 BarButton 设置。

它不会被拉伸(stretch),而且您可以设置与图像相同的宽度/高度分辨率。

希望对您有所帮助。如果我们必须采用其他解决方案,请告诉我。

关于ios - iphone UIBarButtonItem : setBackgroundImage - Stretched,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21933865/

相关文章:

ios - iOS 版 UIBarButtonItem-角标(Badge)

objective-c - 根据计时器提示 AVAudioPlayer 播放

ios - 获取 UIBarButtonItem 的框架返回 nil

ios - xcode 正在创建通用 xcode 存档而不是 iOS App Archive

ios - 如何实现验证现有用户名

ios - 为什么以编程方式创建的(底部)工具栏按钮标题不显示?

ios - 如何编辑 UINavigationBar 中左、右 UIBarButtonItem 的空白区域 [iOS 7]

ios - 移除 UIBarButtonItem 的文本阴影

每个目标和 Storyboard 的 iOS 不同布局

ios - 如何对 UIView 进行自定义布局,使其适用于 iPhone 5 屏幕尺寸以及常规屏幕尺寸