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/3903018/

相关文章:

objective-c - Objective C - 在属性上声明可选字段

ios - 动态传递@selector

ios - View Controller Containment 共享父对象

ios - 如何在 objective-c 中返回对象的 UIColor?

ios - UIImagePickerController 中的左右 barButtons 字体

ios - 以编程方式设置图像的大小

iphone - 有没有一种简单的方法可以查看可按下的链接?

ios - UIView 忽略 XCode 8 中的背景颜色(或布局问题)

ios - 相当于 iOS 上的 Android 服务

iOS:UISplitViewController 后退按钮设置查询