self.ios navigation titleItem.title与self.title有什么区别

iOS(642)
本文试图阐释清楚导航栏相关的概念和用法,比如UINavigationBar和UINavigationItem的区别和联系,UIBarButtonItem的用法以及在纯代码和storyboard中有什么不同。如果读者有类似的疑惑,不妨读一读本文。
本文撰写时,用的iOS8.3、Xcode6.3,因为没有仔细考证iOS各版特性的不同,可能导致出入,若读者遇到,还请指出,我及时改正。
1、UINavigationBar VS UINavigationItem
UINavigationBar继承图
文档说明:
The UINavigationBar class provides a control for navigating hierarchical content. It’s a bar, typically displayed at the top of the screen, containing buttons for navigating within a hierarchy of screens. The primary properties are a left (back) button, a center
title, and an optional right button. You can use a navigation bar as a standalone object or in conjunction with a navigation controller object.
UINavigationBar类提供一种对导航层级内容的控制。它是一个栏,最典型的用法就是放在屏幕顶端,包含着各级视图的导航按钮。它最首要的属性是左按钮(返回按钮)、中心标题,还有可选的右按钮。你可以单独用导航栏,或者和导航控制器一起使用。
UINavigationItem继承图
文档说明:
A UINavigationItem object manages the buttons and views to be displayed in a UINavigationBar object. When building a navigation interface, each view controller pushed onto the navigation stack must have a UINavigationItem object that contains the buttons and
views it wants displayed in the navigation bar. The managing UINavigationController object uses the navigation items of the topmost two view controllers to populate the navigation bar with content.
一个UINavigationItem对象管理展示在导航栏上的按钮和视图。当创建一个导航界面的时候,每个压入导航栈中的视图控制器都需要一个navigation item,它包含了展示在导航栏上的按钮和视图。导航控制器利用最顶层的两个视图控制器的navigation item来提供导航栏的内容。
在纯代码操作UINavigationBar和UINavigationItem的实例中,我们会觉得不舒服,或者说疑惑的地方
事实上,UINavigationController并没有navigationItem这样一个直接的属性,由于UINavigationController继承于UIViewController,而UIViewController是有navigationItem这个属性的,所以才会出现如图所示的情况,如果你这样用:
self.navigationController.navigationItem.title = @&刘大帅&;
是没有任何效果的。这当然是由于UINavigationController是个特殊的视图控制器,它是视图控制器的容器(另外两个容器是UITabBarController和UISplitViewController),你不应该把它当一般的UIViewController来使用.
另外,让人觉得不爽的地方如下:
self.navigationItem.title = @&刘大帅&;
self.navigationController.navigationBar.barTintColor = [UIColor purpleColor];
效果如下:
这里让人迷惑的地方在于,同样是对导航栏的操作,怎么一个在第一层级(UIViewController),另外一个在其属性navigationController的层级。
如前所说,navigationItem是UIViewController的一个属性,开发者文档是这样描述这个属性的:
This is a unique instance of UINavigationItem created to represent the view controller when it is pushed onto a navigation controller. The first time the property is accessed, the UINavigationItem object is created. Therefore, you should not access this property
if you are not using a navigation controller to display the view controller. To ensure the navigation item is configured, you can either override this property and add code to create the bar button items when first accessed or create the items in your view
controller'€™s initialization code.
Avoid tying the creation of bar button items in your navigation item to the creation of your view controller'€™s view. The navigation item of a view controller may be retrieved independently of the view controller'€™s view. For example, when pushing two view
controllers onto a navigation stack, the topmost view controller becomes visible, but the other view controller'€™s navigation item may be retrieved in order to present its back button.
The default behavior is to create a navigation item that displays the view controller'€™s title.
翻译一下:
它是UINavigationItem一个独特的实例。当视图控制器被推倒导航控制器中时,它来代表这个视图控制器。当第一次访问这个属性的时候,它会被创建。因此,如果你并没有用导航控制器来管理视图控制器,那你不应该访问这个属性。为确保navigation item 已经配置,你可以在视图控制器初始化时,重写这个属性、创建bar button item。
要避免在创建视图控制器的视图时,创建bar button item。视图控制器的这个属性——navigationItem,它的恢复(生命周期——作者注),可能独立于视图控制器的视图。为什么会这样?举例来说,当把两个视图控制器压到导航栈中,最顶层的视图控制器是可见的,但另一个视图控制器的navigation item 可能是活跃状态(此时,隐藏的视图控制器的视图肯定是不活跃的,所以,这个时候navigation item 是独立于视图控制器的视图的——作者注),因为它要呈现其返回按钮。
缺省行为是创建一个navigation item 来展示视图控制器的标题。
我们来总结一下,如果把导航控制器比作一个剧院,那导航栏就相当于舞台,舞台必然是属于剧院的,所以,导航栏是导航控制器的一个属性。视图控制器(UIViewController)就相当于一个个剧团,而导航项(navigation item)就相当于每个剧团的负责人,负责与剧院的人接洽沟通。显然,导航项应该是视图控制器的一个属性。虽然导航栏和导航项都在做与导航相关的事情,但是它们的从属是不同的。
我想,这个类比应该能解决以上的疑惑吧。导航栏相当于负责剧院舞台的布景配置,导航项则相当于协调每个在舞台上表演的演员(bar button item,title 等等),每个视图控制器的导航项可能都是不同的,可能一个右边有一个选择照片的bar button item,而另一个视图控制器的右边有两个bar button item。
2、关于UINavigationItem一些测试
我们知道navigation item 有leftBarButtonItems和rightBarButtonItems两个属性,每个属性都可以赋值一个装有UIBarButtonItem对象的数组,有没有想过,如果数组装有很多UIBarButtonItem对象,超过了导航栏展现的极限,会怎样?如下图:
导航栏被撑爆...
NSMutableArray* array = [NSMutableArray array];
for (int i =0; i&7; i++) {
UIBarButtonItem* item = [[UIBarButtonItem alloc]initWithTitle:[NSString stringWithFormat:@&item%d&,i+1] style:UIBarButtonItemStylePlain target:nil action:nil];
[array addObject:item];
self.navigationItem.leftBarButtonItems =
self.navigationItem.rightBarButtonItems =
其实,这在开发文档中已经说的很清楚了,拿leftBarButtonItems来说:
This array can contain 0 or more bar items to display on the left side of the navigation bar. Items can include fixed-width and flexible-width spaces. 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.
If there is not enough room to display all of the items in the array, those that would overlap the title view (if present) or the buttons on the right side of the bar are not displayed.
The first item in the array can also be set using the leftBarButtonItem property.
前面说过,用代码的时候,当你首次访问视图控制器中的navigation item的时候,它会自动创建,在storyboard中是怎样的呢?
答案是,你需要给导航栏中的scene添加navigation item,如图:
没有添加navigation item 之前
添加navigation item 之后
storyboard中怎样配置leftBarButtonItems和rightBarButtonItems两个属性?
我发现storyboard只支持左右各一个bar button item,当你拖拽一个新的bar button item到导航栏视图给它增加一个时,它只会替换,可能,如果想多个,还得用代码来实现。如图:
storyboard只支持左右各一个
3、UIBarButtonItem VS UIButton
其实对于这两个,我没有深入总结。
UIBarButtonItem继承图
UIButton继承图
通过这两个图,我们知道这两个家伙没什么血缘关系,有点像生物界的趋同进化,比如小熊猫和浣熊
小熊猫和浣熊
(例子不太恰当,其实这俩动物区别挺大的……)。
我尝试过用UIButton当UIBarButtonItem使用(通过storyboard将UIButton拖拽到导航栏上,并写了响应事件),button倒是能显示出来,只是点击没反应。这倒不出乎意料,如果能当UIBarButtonItem使用,才应该出乎意料,毕竟它们除了长的样子和交互方式类似,其他并不同。
其实,我们知道UIBarButtonItem是专门给UIToolBar和UINavigationBar定制的类似button的类就好了。将来有更深的体会,我会及时更新。
4、UIToolBar VS UITabBar
这个也没什么较深的体会,先占个位置……
这里之所以提一句,是因为导航控制器带有一个toolBar的属性,在storyboard中,如果你没有给scene添加navigation item,就往scene上拖拽bar button item,它是不会落到导航栏上,而是落到toolBar上,toolBar默认是隐藏的,但在scene上它是显示出来的。
接下来,挂羊头卖狗肉,在这里结合UIToolBar,讲一个UIBarButtonItem的用法——为相邻bar button item添加间隔,通过观察,这个只在UIToolBar中有效果(在storyboard中使用的话,只能给UIToolBar添加,storyboard的对象库,也说明这是为UIToolBar准备的)
没有添加间隔
5、导航栏一般用法集锦
对于导航栏的操作有两种方法:
[UINavigationBar appearance]类方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[UINavigationBar appearance].tintColor = [UIColor orangeColor];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@&m_nav64&] forBarMetrics:UIBarMetricsDefault];
return YES;
这显然是因为UINavigationBar遵从了UIAppearance协议的缘故。这个方法在AppDelegate中有效,在特定的视图控制器中是无效的。它应该是对所有导航栏生效的。
self.navigationController.navigationBar 实例方法
- (void)viewDidLoad
self.navigationItem.title = @&刘大帅&;
self.navigationController.navigationBar.tintColor = [UIColor orangeColor];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@&m_nav&] forBarMetrics:UIBarMetricsDefault];
self.navigationController.toolbarHidden = NO;
这种用法只对该视图控制器的导航栏有效果,由于viewDidLoad:在application: didFinishLaunchingWithOptions:之后执行,所以它会覆盖上一种方法带来的效果。假设这样一种场景,用UITabBarController作为最外层视图控制器容器,每一个tab都有自己的一个导航栈。我们可以用第一种方法做整体效果的设计,用第二种方法作特定tab中的导航栏的设计。
注意:两个效果之所以有区别,是因为我用了不同的图片,以示区别。
下面我们以第二种方法为例来介绍导航栏的一般用法
- (void)viewDidLoad
self.view.backgroundColor = [UIColor orangeColor];
self.navigationItem.title = @&刘大帅&;
self.navigationItem.titleView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@&m_hot60&]];
UIBarButtonItem* item1 = [[UIBarButtonItem alloc]initWithTitle:@&item1& style:UIBarButtonItemStylePlain target:nil action:nil];
UIBarButtonItem* item2 = [[UIBarButtonItem alloc]initWithTitle:@&item2& style:UIBarButtonItemStylePlain target:nil action:nil];
NSArray* array = @[item1,item2];
self.navigationItem.leftBarButtonItems =
self.navigationItem.rightBarButtonItems =
self.navigationController.navigationBar.barTintColor = [UIColor orangeColor];
self.navigationController.navigationBar.translucent = NO;
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@&m_nav64&] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.tintColor = [UIColor purpleColor];
self.navigationController.navigationBar.backIndicatorImage = [UIImage imageNamed:@&m_ios&];
self.navigationController.navigationBar.backIndicatorTransitionMaskImage = [UIImage imageNamed:@&m_ios&];
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
shadow.shadowOffset = CGSizeMake(0, 1);
self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0],
NSShadowAttributeName:shadow,
NSFontAttributeName:[UIFont fontWithName:@&HelveticaNeue-CondensedBlack& size:21.0]
self.navigationController.toolbarHidden = NO;
相关效果图1
相关效果图2
相关效果图3
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:112699次
积分:4813
积分:4813
排名:第4013名
原创:237篇
转载:856篇
译文:18篇
评论:21条
(53)(68)(90)(96)(104)(153)(78)(75)(150)(181)(47)(2)(1)(2)(15)(1)self.navigationItem.title与self.title有什么区别_百度知道
self.navigationItem.title与self.title有什么区别
我有更好的答案
title,要是不在NavigationController呢,不论在哪种控制器里面.title是显示在navgationbar的中间.title?换言之self,可以根据在NavigationController设置navigationItem。要是没有设置。如果在NavigationController里面,都会优先显示self.title是控制器默认view的titleself,那应该是一样的吧.title设置了,或者在TabbarController设置tabBarItem.navigationItemself.title? 在TabbarController里面呢
self.title是控制器默认view的titleself.navigationItem.title是显示在navgationbar的中间。如果在NavigationController里面,那应该是一样的吧,要是不在NavigationController呢? 在TabbarController里面呢?换言之self.title设置了,不论在哪种控制器里面,都会优先显示self.title。要是没有设置,可以根据在NavigationController设置navigationItem.title,或者在TabbarController设置tabBarItem.title;
Self navigation Item title 应该是自我导航物品名称?Self title 是自我名称?(好吧其实我也不知道[哭)
其他类似问题
为您推荐:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁12891人阅读
Iphone开发基础入门(19)
摘自社会成员的cnblog,原文链接如下:/lsck/archive//2508878.html
3、设置标题:
打开ViewController.m,在viewDidLoad方法中[super viewDidLoad];之后添加代码:
self.navigationItem.title = @&标题&;
4、自定义标题,设置titleView:
如果我们想改变标题的颜色和字体,就需要自己定义一个UILabel,并且已经设置好这个Label的内容,可以设置自己想要的字体、大小和颜色等。然后执行self.navigationItem.titleView = myL就可以看到想要的效果。
4.1 打开ViewController.h,向其中添加属性:
@property (strong, nonatomic) UILabel *titleL
4.2 打开ViewController.m,在@implementation ViewController下面一行添加代码:
@synthesize titleL
4.3 在viewDidLoad方法中,去掉self.navigationItem.title = @&标题&;,并添加代码:
//自定义标题
titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0 , 100, 44)];
titleLabel.backgroundColor = [UIColor clearColor];
//设置Label背景透明
titleLabel.font = [UIFont boldSystemFontOfSize:20];
//设置文本字体与大小
titleLabel.textColor = [UIColor colorWithRed:(0.0/255.0) green:(255.0 / 255.0) blue:(0.0 / 255.0) alpha:1];
//设置文本颜色
titleLabel.textAlignment = UITextAlignmentC
titleLabel.text = @&自定义标题&;
//设置标题
self.navigationItem.titleView = self.titleL
实际上,不仅仅可以将titleView设置成Label,只要是UIView的对象都可以设为titleView,例如,将4.3中的代码改成:
UIButton *button = [UIButtonbuttonWithType: UIButtonTypeRoundedRect];
[button setTitle: @&按钮& forState: UIControlStateNormal];
[button sizeToFit];
self.navigationItem.titleView =
则运行起来效果如下:
5、为Navigation Bar添加左按钮
以下是进行leftBarButtonItem设置的代码:
self.navigationItem.leftBarButtonItem = (UIBarButtonItem *)
self.navigationItem.leftBarButtonItems = (UIBarButtonItem *)
self.navigationItemsetLeftBarButtonItem:(UIBarButtonItem *)
self.navigationItemsetLeftBarButtonItem:(UIBarButtonItem *) animated:(BOOL)
self.navigationItemsetLeftBarButtonItems:(NSArray *)
self.navigationItemsetLeftBarButtonItems:(NSArray *) animated:(BOOL)
其实很简单,只要定义好一个UIBarButtonItem,然后执行上述某行代码就行了。
5.1 为了使得运行时不出错,我们在ViewController.m中添加一个空方法,由将要创建的左右按钮使用:
-(void)myAction {
5.2 添加一个左按钮:
在ViewDidLoad方法最后添加代码:
//添加左按钮
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]
initWithTitle:@&左按钮&
style:UIBarButtonItemStylePlain
target:self
action:@selector(myAction)];
[self.navigationItem setLeftBarButtonItem:leftButton];
运行效果如下:
创建一个UIBarButtonItem用的方法主要有:
[UIBarButtonItemalloc]initWithTitle:(NSString *) style:(UIBarButtonItemStyle) target:(id) action:(SEL)
[UIBarButtonItemalloc]initWithBarButtonSystemItem:(UIBarButtonSystemItem) target:(id) action:(SEL)
在第一个方法中,我们可以使用的按钮样式有:
UIBarButtonItemStyleBordered
UIBarButtonItemStyleDone
UIBarButtonItemStylePlain
效果分别如下:
看上去第一个和第三个样式效果是一样的。
6、添加一个右按钮
在ViewDidLoad方法最后添加代码:
//添加右按钮
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemUndo
target:self
action:@selector(myAction)];
self.navigationItem.rightBarButtonItem = rightB
运行如下:
这里创建UIBarButtonItem用的方法是
[UIBarButtonItemalloc]initWithBarButtonSystemItem:(UIBarButtonSystemItem) target:(id) action:(SEL)
用了系统自带的按钮样式,这些样式的标签和效果如下:
& & & & & & & & & 标签
& & & 效果
& & & & & & & & & & & &标签
& & & & &效果
UIBarButtonSystemItemAction
& & & & UIBarButtonSystemItemPause
UIBarButtonSystemItemAdd
& & & & UIBarButtonSystemItemPlay
UIBarButtonSystemItemBookmarks
& & & & UIBarButtonSystemItemRedo
UIBarButtonSystemItemCamera
& & & & UIBarButtonSystemItemRefresh
UIBarButtonSystemItemCancel
& & & & UIBarButtonSystemItemReply
UIBarButtonSystemItemCompose
& & & & UIBarButtonSystemItemRewind
UIBarButtonSystemItemDone
& & & & UIBarButtonSystemItemSave
UIBarButtonSystemItemEdit
& & & & UIBarButtonSystemItemSearch
UIBarButtonSystemItemFastForward
& & & & UIBarButtonSystemItemStop
UIBarButtonSystemItemOrganize
& & & & UIBarButtonSystemItemTrash
UIBarButtonSystemItemPageCurl
& & & & UIBarButtonSystemItemUndo
注意,UIBarButtonSystemItemPageCurl只能在Tool Bar上显示。
7、添加多个右按钮
在ViewDidLoad方法中最后添加代码:
//添加多个右按钮
UIBarButtonItem *rightButton1 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(myAction)];
UIBarButtonItem *rightButton2 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
UIBarButtonItem *rightButton3 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
target:self
action:@selector(myAction)];
UIBarButtonItem *rightButton4 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
UIBarButtonItem *rightButton5 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize
target:self
action:@selector(myAction)];
NSArray *buttonArray = [[NSArray alloc]
initWithObjects:rightButton1,rightButton2,
rightButton3,rightButton4,rightButton5, nil];
self.navigationItem.rightBarButtonItems = buttonA
为了更好的显示效果,把设置titleView以及设置leftBarButtonItem的代码注释掉,运行效果如下:
上面的UIBarButtonSystemItemFixedSpace和UIBarButtonSystemItemFlexibleSpace都是系统提供的用于占位的按钮样式。
8、设置Navigation Bar背景颜色
在viewDidLoad方法后面添加代码:
//设置Navigation Bar颜色
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:(218.0/255.0) green:(228.0 / 255.0) blue:(250.0 / 255.0) alpha:1];
运行如下:
9、设置Navigation Bar背景图片
首先将准备好作为背景的图片拖到工程中,我用的图片名称是title_bg.png。
将上面的代码改成:
//设置Navigation Bar背景图片
UIImage *title_bg = [UIImage imageNamed:@&title_bg.png&];
//获取图片
CGSize titleSize = self.navigationController.navigationBar.bounds.
//获取Navigation Bar的位置和大小
title_bg = [self scaleToSize:title_bg size:titleSize];//设置图片的大小与Navigation Bar相同
[self.navigationController.navigationBar
setBackgroundImage:title_bg
forBarMetrics:UIBarMetricsDefault];
//设置背景
之后,在ViewController.m中添加一个方法用于调整图片大小:
//调整图片大小
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
UIGraphicsBeginImageContext(size);
[img drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledI
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:676963次
积分:6289
积分:6289
排名:第2614名
原创:59篇
转载:10篇
评论:244条
文章:11篇
阅读:34788
文章:14篇
阅读:151874
文章:14篇
阅读:149782iOS设置[self.navigationItem setTitleView:View] - 博客频道 - CSDN.NET
Citrus blog
分类:iOS【开发积累】
功能需求在NavigationBar上添加搜索框,并对其位置提出了要求,系统中自带的TitleView不能满足,因此查阅了资料,重写了TitleView
TitleView.h文件,重写TitleView继承UIView
#import &UIKit/UIKit.h&
@interface TitleView : UIView
TitleView.m文件,重写其父类的Frame
#import "TitleView.h"
@implementation TitleView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
return self;
- (void)setFrame:(CGRect)frame {
[super setFrame:CGRectMake(0, 0, self.superview.frame.size.width, self.superview.bounds.size.height)];
在需要使用的地方引用
UIBarButtonItem * backButtonItem = [[UIBarButtonItem alloc] init];
[backButtonItem setTitle:@""];
self.navigationItem.leftBarButtonItem = backButtonI
_titleView = [[TitleView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
_titleView.backgroundColor = [UIColor blackColor];
self.navigationItem.titleView = _titleV
排名:千里之外
(15)(3)(6)(1)(1)(1)(1)

我要回帖

更多关于 self.navigationitem 的文章

 

随机推荐