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

09-26 17:41