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

发布于 2024-09-27 01:24:02 字数 56 浏览 6 评论 0原文

当我尝试对 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

诠释孤独 2024-10-04 01:24:02

您可以使用具有图像和文本的自定义视图来初始化 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];

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];
一念一轮回 2024-10-04 01:24:02

我建议一种使用情节提要来执行此操作的方法:

  1. 拖放到工具栏上通常的 UIView。它会自动包装到 Bar Button Item。在“尺寸检查器”中调整视图宽度。将视图的背景颜色更改为透明颜色。

输入图像描述这里

  1. 将 UIButton 和 UILabel 放入视图中。您可以使用约束来调整它们的位置。您也可以将其放在视图之外,例如稍高或稍低(如我的图片所示)。放置 UIButton 的默认图像和突出显示图像。

  2. 复制栏按钮项目并调整其他按钮。添加灵活的空间。

  3. 创建销售点。

  4. 完成:)。当您运行应用程序时,您会看到如下所示的工具栏:

在此处输入图像描述

PS 此处< /a> 您可以阅读如何使用 .xib 创建 UIToolbar 子类

I suggest a way how to do this using Storyboard:

  1. Drag and drop to ToolBar usual UIView. It automatically will be wrapped to Bar Button Item. Adjust View width in "Size inspector". Change background color of View to clear color.

enter image description here

  1. Put UIButton and UILabel inside a View. You can use constraints to adjust their positions. Also you can put thier outside of a View, for example, little higher or less (like on my picture). Put Default and Highlighted images for UIButton.

  2. Copy Bar Button Item and adjust another buttons. Add Flexible spaces.

  3. Create outlets.

  4. Done :). When you run app, you get Tool Bar like this:

enter image description here

P.S. Here you can read how to create UIToolbar subclass using .xib

千纸鹤带着心事 2024-10-04 01:24:02

带有目标的 Swift 4.2

extension UIBarButtonItem {
    convenience init(image :UIImage, title :String, target: Any?, action: Selector?) {
        let button = UIButton(type: .custom)
        button.setImage(image, for: .normal)
        button.setTitle(title, for: .normal)
        button.frame = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)

        if let target = target, let action = action {
            button.addTarget(target, action: action, for: .touchUpInside)
        }

        self.init(customView: button)
    }
}

Swift 4.2 with target

extension UIBarButtonItem {
    convenience init(image :UIImage, title :String, target: Any?, action: Selector?) {
        let button = UIButton(type: .custom)
        button.setImage(image, for: .normal)
        button.setTitle(title, for: .normal)
        button.frame = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)

        if let target = target, let action = action {
            button.addTarget(target, action: action, for: .touchUpInside)
        }

        self.init(customView: button)
    }
}
旧情别恋 2024-10-04 01:24:02
UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
[button addTarget:target action:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 53, 31)];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(3, 5, 50, 20)];
[label setFont:[UIFont fontWithName:@"Arial-BoldMT" size:13]];
[label setText:title];
label.textAlignment = UITextAlignmentCenter;
[label setTextColor:[UIColor whiteColor]];
[label setBackgroundColor:[UIColor clearColor]];
[button addSubview:label];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = barButton;
UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
[button addTarget:target action:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 53, 31)];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(3, 5, 50, 20)];
[label setFont:[UIFont fontWithName:@"Arial-BoldMT" size:13]];
[label setText:title];
label.textAlignment = UITextAlignmentCenter;
[label setTextColor:[UIColor whiteColor]];
[label setBackgroundColor:[UIColor clearColor]];
[button addSubview:label];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = barButton;
违心° 2024-10-04 01:24:02

Kris Markel 的答案是一个好的开始(仅< iOS7),但是如果您使用色调颜色,则它不适用于图像和按钮的突出显示状态。

我创建了一个类别,它将创建一个 UIBarButtonItem (leftBarButtonItem),其外观和行为类似于普通的 iOS7 后退按钮。看一下: https://github.com/Zero3nna/UIBarButtonItem-DefaultBackButton

Kris Markel's answer is a good start (only < iOS7), but if you use tint color, it wount work for the image and the highlighted state of the button.

I've created a category that will create a UIBarButtonItem (leftBarButtonItem) that looks and behaves like a normal iOS7 back button. Have a look at: https://github.com/Zero3nna/UIBarButtonItem-DefaultBackButton

醉态萌生 2024-10-04 01:24:02

快速更新 UIBarButtonItem 具有包含图像和文本的自定义视图。

var rightImage: UIImage  = UIImage(named: "YourImageName")
var rightButton : UIButton = UIButton(type: UIButtonType.custom)
rightButton.setBackgroundImage(rightImage, for: .normal)
rightButton.setTitle(title, for: .normal)
rightButton.frame.size = CGSize(width: 100, height: 30)
        
let menuUIBarButtonItem = UIBarButtonItem(customView: rightButton)

Swift Update for UIBarButtonItem with a custom view that has both image and text.

var rightImage: UIImage  = UIImage(named: "YourImageName")
var rightButton : UIButton = UIButton(type: UIButtonType.custom)
rightButton.setBackgroundImage(rightImage, for: .normal)
rightButton.setTitle(title, for: .normal)
rightButton.frame.size = CGSize(width: 100, height: 30)
        
let menuUIBarButtonItem = UIBarButtonItem(customView: rightButton)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文