在iPhone导航栏上绘制自定义后退按钮

在iPhone导航栏上绘制自定义后退按钮

本文介绍了在iPhone导航栏上绘制自定义后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有NavigationBar的UIView,我想添加一个看起来像后退按钮样式的按钮。我没有使用UINavigationController并且想知道是否可以在没有它的情况下完成它?

I've got a UIView with a NavigationBar and I'd like to add a button which looks like the style of a Back Button. I'm not using a UINavigationController and was wondering if this could be done without it?

按钮的样式应如下所示:

The style of the button should look like this:

谢谢

推荐答案

您需要设置一个 UINavigationItem 对象的自定义堆栈,并将它们推送到UINavigationBar。这是我知道获得真正后退按钮的唯一方法。我没有测试过这段代码,但你应该这样做:

You need to set up a custom stack of UINavigationItem objects and push them on to the UINavigationBar. This is the only way I know of to get a true back button. I haven't tested this code, but you should do something like this:

UINavigationItem *previousItem =
    [[[UINavigationItem alloc] initWithTitle:@"Back title"] autorelease];

UINavigationItem *currentItem =
    [[[UINavigationItem alloc] initWithTitle:@"Main Title"] autorelease];

[navigationBar setItems:[NSArray arrayWithObjects:previousItem, currentItem, nil]
               animated:YES];

要处理按钮按下时你应该将自己设置为导航栏的委托并实现UINavigationBarDelegate委托。

To handle when the buttons are pressed you should set yourself as the navigation bar's delegate and implement the UINavigationBarDelegate delegates.

这篇关于在iPhone导航栏上绘制自定义后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:06