objective-c - 我怎样才能拥有一个同时包含图像和文本的 UIBarButtonItem?

标签 objective-c cocoa-touch uibarbuttonitem

当我尝试对 UIBarButtonItem 使用图像时,不显示文本。有没有办法同时显示文本和图像?

最佳答案

您可以使用具有图像和文本的自定义 View 来初始化 UIBarButtonItem。这是使用 UIButton 的示例。

UIImage *chatImage = [UIImage imageNamed:@"08-chat.png"];

UIButton *chatButton = [UIButton buttonWithType:UIButtonTypeCustom];
[chatButton setBackgroundImage:chatImage forState:UIControlStateNormal];
[chatButton setTitle:@"Chat" forState:UIControlStateNormal];
chatButton.frame = (CGRect) {
    .size.width = 100,
    .size.height = 30,
};

UIBarButtonItem *barButton= [[[UIBarButtonItem alloc] initWithCustomView:chatButton] autorelease];
self.toolbar.items = [NSArray arrayWithObject:barButton];

关于objective-c - 我怎样才能拥有一个同时包含图像和文本的 UIBarButtonItem?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48885939/

相关文章:

iphone - 哪些脚本语言解释器可以在 iOS 上运行?

iphone - 如何从 json 对象中提取实际的 NSString 作为 NSArray

ios - 为什么我无法使用带有自动布局的渐变子层来定位我的 View ?它没有出现

iphone - 如何将搜索栏放入UIPickerview的各个组件中?以及如何在每个相关搜索栏中执行搜索?

ios - 显示 UIBarButtonItem 菜单时获取事件

ios - UIView 在 iOS7 中损坏但在 iOS8 中没有

ios - Apple Watch 强制触控菜单

ios - Cocoa Touch Framework 架构问题/生产构建

ios - 调整 UITextField 的宽度以填充横向工具栏

ios - 通过外观为 UIBarButtonItem 设置背景图像不适用于其他 UIControlStates