初探UINavigationItem面目

来源:互联网 发布:淘宝双11不能付款 编辑:程序博客网 时间:2024/05/12 07:53

近日开始做ios界面,在用到UINavigationBar的时候遇到一些问题,在这里做下笔记。

在使用UINavigationBar之前,需要先了解另外一个类,UINavigationItem。每个VC(即UIViewController)都有一个UINavigation属性,用以保存对应navigation bar的属性值,当VC被navigationController压入栈后,navigationController就会读取这个VC的navigationItem的值,然后显示到navigationBar上面。那么,navigationItem有哪些属性呢?

1、title,很明显,这是显示在navigationBar中间的标题字符串,在VC中可以这样设置:

<span style="white-space:pre"></span>self.navigationItem.title = @"firstVC";
设置这个title,不仅在VC中能看到标题,当这个VC跳转到下一个VC时,在下一个VC的返回按钮也会显示这个title内容,如图:


那么,也许有些情况下你并不希望下第二个VC中显示“firstVC”这个title,或者是想显示其他字符串,那要怎么设置呢?这就要接着看navigationItem的属性了。

2、backBarButtonItem,官方文档这么解释:Bar button item to use for the back button in the child navigation item.这是一个UIBarButtonItem对象,在子navigationItem中将通过这个对象的属性来显示返回按钮。在上面例子中,我们可以在firstVC中设置backBarButtonItem的title属性,来改变secondVC的返回按钮文字。

//在firstVC中设置backBarButtonItemUIBarButtonItem *firstBackButton = [[UIBarButtonItem alloc]init];firstBackButton.title = @"test";self.navigationItem.backBarButtonItem = firstBackButton;

效果如图

 

当然,UIBarButtonItem不止title这个属性,其他属性可参考apple官方文档,此处暂不深究。

3、prompt,A single line of text displayed at the top of the navigation bar,在navigation bar顶部显示的一行字符串,效果如下,


4、hidesBackButton,这个属性用来设置是否显示左上角的返回按钮,比如上述例子中,secondVC有一个返回按钮(firstVC是在栈底的根视图控制器,所以并没有返回按钮),我们可以在secondVC里设置hidesBackButton为YES:

self.navigationItem.hidesBackButton = YES;
运行效果如图



以上都是一些可以改变显示内容的属性,接下来看下navigationItem里面的自定义视图属性

1、titleView,显而易见这是显示标题的视图,如果我们没设置它,它默认为nil,那么navigationBar就会去读取navigationItem.title属性的值然后显示出来,上述例子中我们设置title属性后能显示出来,是因为我们没有设置titleView。那我们现在设置一下titleView,看下效果怎样:

UILabel *titleLabel = [[UILabel alloc] initWithFrame:(CGRect){0,0,0,20}];//这里只需设置高度,宽度是灵活的titleLabel.text = @"this is titleView";self.navigationItem.titleView = titleLabel;self.navigationItem.title = @"secondVC";
这里我们同时设置了titleView和title,你猜会显示哪个?效果如下:



2、leftBarButtonItems,An array of custom bar button items to display on the left side of the navigation bar when the receiver is the top navigation item.

这是一个UIBarButtonItem数组,如果不为空,那么navigation Bar 将从左往右依次显示出这些item,下面用代码测试一下:

    UIBarButtonItem *item1 = [[UIBarButtonItem alloc]init ];    item1.title = @"item1";    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] init];    item2.title = @"item2";    item2.tintColor = [UIColor redColor];    NSMutableArray *array = [[NSMutableArray alloc] init];    [array addObject:item1];    [array addObject:item2];    self.navigationItem.leftBarButtonItems = array;

效果如图:

那么,为什么返回按钮会不见了呢?官方文档是这么说的: If the leftItemsSupplementBackButton property is YES, the items are displayed to the right of the back button, otherwise the items replace the back button and start at the left edge of the bar. Items are displayed left-to-right in the same order as they appear in the array.意思是如果leftItemsSupplementBackButton这个属性为YES的话,这些item就会显示在返回按钮的右边,否则则会覆盖掉返回按钮。既然如此,那我们将leftItemSupplementBackButton设为YES试试看:

self.navigationItem.leftItemsSupplementBackButton = YES;

效果如图:

可以看到,如果leftBarButtonItems数组里item太多的话,它们会占据titleview的位置。

3、leftBarButtonItem,这个就是表示leftBarButtonItems里的第一个item。

还有rightBarButtonItems,rightBarButtonItem,道理跟上述类似,就不在赘述了。

这里对UINavigationItem的探索就到此为止,后续将补充UINavigationBar的有关内容。



0 0
原创粉丝点击