本文介绍了如何拥有同时包含图像和文本的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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!