如何有一个UIBarButtonItem与图像和文本 [英] How to have a UIBarButtonItem with both image and text

查看:116
本文介绍了如何有一个UIBarButtonItem与图像和文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用图像作为UIBarButtonItem时,文本不会显示。是否有方法来显示文本和图像?

When I try to use an image for a UIBarButtonItem, the text isn't shown. Is there a way to show both the text and the image?

推荐答案

您可以使用自定义视图来初始化UIBarButtonItem,图像和文本。下面是一个使用UIButton的示例。

You can init the UIBarButtonItem with a custom view that has both image and text. Here's a sample that uses a 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];

这篇关于如何有一个UIBarButtonItem与图像和文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆