iPhone:制作箭头形状的UIBarButtonItem [英] iPhone : making UIBarButtonItem that is arrow shaped

查看:92
本文介绍了iPhone:制作箭头形状的UIBarButtonItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在导航栏上有一个UIBarButtonItem。我想把它做成箭头形状。我的意思是我希望它是正方形,除了尖尖的一面。有谁知道怎么做?

I have a UIBarButtonItem on a navigation bar. I'd like to make it arrow shaped. By that I mean I want it to be square except for a pointy side. Does anyone know how to do this?

谢谢!

推荐答案

我在这个问题上采用了几种方法,并最终使用来自多个来源的所有答案进行了解决。有几个技巧;这个片段会显示所有内容(我正在创建一个自定义 rightNavButton ,但您可以轻松地适应任何 UIBarButtonItem ) :

I wrestled with several approaches on this one and finally figured it out using all the answers from several sources. There are a couple of tricks; this snippet will show it all (I was creating a custom rightNavButton, but you can adapt this easily for any UIBarButtonItem):

// Produces a nice "right arrow" style button that mirrors the back arrow
// automatically added by the navController
//
UIImage *buttonImage = [UIImage imageNamed:@"forwardButton.png"];
UIButton *forwardButton = [UIButton buttonWithType:UIButtonTypeCustom];
[forwardButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[forwardButton setTitle:@"Meter" forState:UIControlStateNormal];
forwardButton.font = [UIFont boldSystemFontOfSize:12];
forwardButton.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[forwardButton addTarget:self action:@selector(showMeter) 
           forControlEvents:UIControlEventTouchUpInside];

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] 
           initWithCustomView:forwardButton];

注意:如果你试图直接在按钮项上设置目标和动作使用 UIButton 分配自定义视图,它不需要设置 - 你必须在按钮上设置目标和操作 - 显然是 navBar 使用逐字提供的按钮。

Note: it turns out that if you try to set the target and action directly on the button item after assigning a custom view using a UIButton that it won't take - you have to set the target and action on the button itself - apparently the navBar uses the button provided verbatim.

这是我在不透明的黑色 navBar上使用的图像(显然你可以使用任何东西): http://raretiger.com/images/ forwardbutton.png

This is the image I was using on the opaque black navBar (you can use anything, obviously): http://raretiger.com/images/forwardbutton.png

这篇关于iPhone:制作箭头形状的UIBarButtonItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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