问题描述
我在导航栏上有一个 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!