IOS 改变Navigation的返回按钮

时间:2014-07-05 20:30:07   收藏:0   阅读:337

两个办法:

1, 手动为每一个UIViewController添加navigationItem的leftButton的设置代码

2,为UINavigationController实现delegate,在pop和push的时候改变当前和上一页的navigationItem.title

以下是封装的一些基础方法,供参考:

+ (void) navigationItem:(UINavigationItem*)navigationItem 

               setTitle:(NSString*)title;

+ (void) navigationItem:(UINavigationItem*)navigationItem

          setBackButton:(NSString*)title

              addTarget:(id)target

                 action:(SEL)action;

+ (void) navigationItem:(UINavigationItem*)navigationItem 

               setImage:(NSString*)imageString;

无奈之下,只好研读UINavigationController Class Reference去,在“Updating the Navigation Bar”小节,有这么一段话:

The bar button item on the left side of the navigation bar allows for navigation back to the previous view controller on the navigation stack. The navigation controller updates the left side of the navigation bar as follows:

我大致解释一下,使用pushViewController切换到下一个视图时,navigation controller按照以下3条顺序更改导航栏的左侧按钮。

1、如果B视图有一个自定义的左侧按钮(leftBarButtonItem),则会显示这个自定义按钮;

2、如果B没有自定义按钮,但是A视图的backBarButtonItem属性有自定义项,则显示这个自定义项;

3、如果前2条都没有,则默认显示一个后退按钮,后退按钮的标题是A视图的标题。

按照这个解释,我把UIBarButtonItem *backItem……这段代码放在A视图的pushViewController语句之前。

OK问题解决了,B视图的后退按钮的标题变成back了。

eg:

UIBarButtonItem*backItem=[[UIBarButtonItemalloc]init];

    backItem.title=@"戻る";

    self.navigationItem.backBarButtonItem=backItem;

    [backItem release]

==============

新方案:

self.navigationController.navigationBar.topItem.title=self.message;

    self.navigationController.navigationBar.tintColor=[UIColorblackColor];

    UIBarButtonItem*backButton = [[UIBarButtonItemalloc] initWithTitle:@" fan hui "style:UIBarButtonItemStyleBorderedtarget:selfaction:@selector(PopViewController)];

    self.navigationItem.leftBarButtonItem= backButton;

以上转自:http://hi.baidu.com/myyuchong/item/1e6db306c5d34de1359902a1

PS:在第三种情况的时候,自动创建的返回按钮只取了A视图的title值,其他属性并没有复制,看来要改变颜色等别的属性还得自定义一个返回按钮啊

IOS 改变Navigation的返回按钮,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!