xcode iPhone:制作箭头形状的 UIBarButtonItem

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2330157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:17:51  来源:igfitidea点击:

iPhone : making UIBarButtonItem that is arrow shaped

iphonexcodeuinavigationcontrolleruibarbuttonitem

提问by WoodenKitty

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?

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

Thanks!

谢谢!

回答by Huygir

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):

我在这个问题上尝试了几种方法,最后使用来自多个来源的所有答案找到了它。有几个技巧;此代码段将显示所有内容(我正在创建一个自定义rightNavButton,但您可以轻松地将其调整为任何内容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];

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 UIButtonthat it won't take - you have to set the target and action on the button itself - apparently the navBaruses the button provided verbatim.

注意:事实证明,如果您尝试在使用UIButton它不会采取的自定义视图分配自定义视图后直接在按钮项上设置目标和操作- 您必须在按钮本身上设置目标和操作 - 显然是navBar使用该按钮逐字提供。

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

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

回答by kennytm

Make a custom background. See Creating a left-arrow button (like UINavigationBar's "back" style) on a UIToolbarfor how.

制作自定义背景。请参阅在 UIToolbar 上创建左箭头按钮(如 UINavigationBar 的“后退”样式)了解如何操作。



You may -pushNavigationItem:animated:to make the built-in back button appear, although you cannot assign custom actions to it.

您可以-pushNavigationItem:animated:使内置的后退按钮出现,但您不能为其分配自定义操作。



For undocumented methods, you may use

对于未记录的方法,您可以使用

UIButton* backButton = [UIButton buttonWithType:101];

to create a back "button".

创建一个后退“按钮”。

回答by Plamen Dragozov

You could create an UIImageView or an UIButton with the required image then use:

您可以使用所需的图像创建 UIImageView 或 UIButton 然后使用:

[[UIBarButtonItem alloc] initWithCustomView: arrowButton];

Hope this helps.

希望这可以帮助。

回答by Ramuel

continuing on @Plamen Dragozov's idea: i noticed if you leave the uibutton as is, it would trigger a picture adjustment when touched and that is quite the opposite what a normal navigation back button does. So what I did was to uncheck "highlighted adjusts Image" and it works like a charm.

继续@Plamen Dragozov 的想法:我注意到如果你保持 uibutton 不变,它会在触摸时触发图片调整,这与普通导航后退按钮的作用完全相反。所以我所做的是取消选中“突出显示的调整图像”,它就像一个魅力。

hope this helps newbies like me.

希望这可以帮助像我这样的新手。

回答by mukaissi

More elegant solution:

更优雅的解决方案:

UIBarButtonItem * backButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Title" style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
[backButtonItem setBackgroundImage:[UIImage imageNamed:@"back.png"]      forState:UIControlStateNormal     barMetrics:UIBarMetricsDefault];
self.navigationItem.leftBarButtonItem = backButtonItem;

回答by Ege Akpinar

My problem was renaming the back button that appears on the pushed view controller. I found a dirty workaround and if you ignore the non-ideal animation problem, you'll get a back button with the title you want.

我的问题是重命名出现在推送视图控制器上的后退按钮。我找到了一个肮脏的解决方法,如果你忽略了非理想的动画问题,你会得到一个带有你想要的标题的后退按钮。

The trick is to change the title of the first VC in viewWillDisappearand re-set it of course in viewWillAppear

诀窍是更改第一个 VC 的标题viewWillDisappear并重新设置它当然viewWillAppear

(Needless to say, by default, if there is no leftBarButtonItemset, UINavigationControllerwill show a back button with the title of the VC that pushed the current VC)

(不用说,默认情况下,如果没有leftBarButtonItem设置,UINavigationController会显示一个后退按钮,上面有推送当前VC的VC的标题)

In the VC where you push your current VC, do

在推送当前 VC 的 VC 中,执行

-(void) viewWillDisappear:(BOOL) animated {
  [super viewWillDisappear:animated];
  self.title = @"Back";
}

-(void) viewWillAppear:(BOOL) animated {
  [super viewWillAppear:animated];
  self.title = @"Original Title";
}