可以在导航栏中为 UIBarButtonItem 设置背景图像,但不能为底部工具栏设置 [英] Can set background image for UIBarButtonItem in nav bar, but not bottom toolbar

查看:22
本文介绍了可以在导航栏中为 UIBarButtonItem 设置背景图像,但不能为底部工具栏设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这在将项目添加到导航栏时有效,但是当我通过界面构建​​器将其添加到底部栏中设置的工具栏时,背景图像不显示.

So this works when adding the item to the nav bar, but when i add it to the toolbar set in the bottom bar via interface builder, the background image doesn't show up.

UIBarButtonItem *resetButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Reset" style:UIBarButtonItemStylePlain target:self action:@selector(resetCriteria:)];
UIImage *img = [UIImage imageNamed:@"someImage.png"];
    img = [img stretchableImageWithLeftCapWidth:5 topCapHeight:20];
    [resetButtonItem setBackgroundImage:img forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

self.toolbarItems   = [NSArray arrayWithObjects: resetButtonItem, nil];

不仅背景没有出现,其他任何行为都不能正常工作(但在将这些栏按钮项添加到导航栏时它们可以正常工作)

Not only does the background not appear, none of the other behaviors work as well (but they work fine when adding these barbutton items to the nav bar)

推荐答案

您应该尝试以下方法来做同样的事情.

You should try the following method to do the same.

在 UIBarButtonItem 中设置自定义视图.

Setting custom view in UIBarButtonItem.

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, 30, 30);
[btn setImage:[UIImage imageNamed:@"someImage.png"] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(resetCriteria:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *Item = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.toolbarItems   = [NSArray arrayWithObject:Item];
[Item release];

我们可以使用 CustomView init 方法在 UIBarButtonItem 中设置任何视图.我们可以设置 ImageView.但是 ImageView 不处理 UIControl 事件.

We can set any view in UIBarButtonItem with CustomView init method. We could set the ImageView. But ImageView doesnot handle UIControl Events.

这篇关于可以在导航栏中为 UIBarButtonItem 设置背景图像,但不能为底部工具栏设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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