[[UIBarButtonItem alloc] initWithTitle与[[UIBarButtonItem alloc] initWithBarButtonSystemItem的区别

总结的区别是:[[UIBarButtonItem alloc] initWithTitle主要用于定义按钮的文字标题,[[UIBarButtonItem alloc] initWithBarButtonSystemItem主要用于定义按钮的图片(不能定义标题)。


我们看下这两种初始化方法在苹果开发文档中是如何定义的:

一、

[[UIBarButtonItem alloc] initWithTitle:style:target:action

从字面理解可知,initWithTitle:style:target:action接口用于定义UIBarButtonItem的标题(title)、按钮样式(只能使用UIBarButtonItemStyle枚举中定义的)与点击事件

---------------------------------------------------------------

从上方文档截图可以看出,按钮样式定义在UIBarButtonItemStyle枚举中。我们来看看苹果开发文档如何定义UIBarButtonItemStyle枚举:


可见只有三种样式可选。它们在外观上的区别如下(摘自:http://dev.son1c.com/show/2596.html):

(UIBarButtonItemStylePlain,默认按钮风格)                   

(UIBarButtonItemStyleBordered)                                    

(UIBarButtonItemStyleDone)                                             

三种样式在表示意义上的区别(参考自:http://book.51cto.com/art/201003/186427.htm):

UIBarButtonItemStylePlain - 按下时会闪动  (这个需要考证,因为本人目前没有iPhone手机可以测试)

UIBarButtonItemStyleBordered - 与UIBarButtonItemStylePlain相同,但显示的按钮有边框  (经测试,这个观点是错误的——把两个按钮截图放大,用PhotoShop的吸管工具吸取两个按钮对应位置的像素,发现颜色值完全相同)

UIBarButtonItemStyleDone - 提醒用户编辑完毕时应该点触(tap)该按钮


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


二、

[[UIBarButtonItem alloc] initWithBarButtonSystemItem:target:action


从字面理解可知,initWithBarButtonSystemItem:target:action:接口用于定义UIBarButtonItem的图片内容(只能使用UIBarButtonSytemItem枚举中定义的)与点击事件。

从上方文档截图可以看出,按钮样式定义在UIBarButtonItemItem枚举中。我们来看看苹果开发文档如何定义UIBarButtonItemStyle枚举:


从外观来说明每个枚举值(摘自:http://dev.son1c.com/show/2596.html):

 
 
 
                  标签      效果                       标签         效果
UIBarButtonSystemItemAction            UIBarButtonSystemItemPause        
UIBarButtonSystemItemAdd            UIBarButtonSystemItemPlay        
UIBarButtonSystemItemBookmarks            UIBarButtonSystemItemRedo        
UIBarButtonSystemItemCamera            UIBarButtonSystemItemRefresh        
UIBarButtonSystemItemCancel            UIBarButtonSystemItemReply        
UIBarButtonSystemItemCompose            UIBarButtonSystemItemRewind        
UIBarButtonSystemItemDone            UIBarButtonSystemItemSave        
UIBarButtonSystemItemEdit            UIBarButtonSystemItemSearch        
UIBarButtonSystemItemFastForward            UIBarButtonSystemItemStop        
UIBarButtonSystemItemOrganize            UIBarButtonSystemItemTrash        
UIBarButtonSystemItemPageCurl            UIBarButtonSystemItemUndo        

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


要注意,[[UIBarButtonItem alloc] initWithTitle与[[UIBarButtonItem alloc] initWithBarButtonSystemItem的区别还在于:

一个UIBarButtonItem,如果此按钮是赋值给self.navigationItem.backBarButtonItem,也就是说定义了一个返回按钮,那么使用[[UIBarButtonItem alloc] initWithTitle才能看到自定义的效果(虽然两种初始化方式在编译时都能通过),并且只有title参数生效,外观依然为系统默认的返回按钮。


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

关于返回按钮(self.navigationItem.backBarButtonItem):


无论使用哪个初始化方法,定义的返回按钮将显示在“下一个视图”,而不是当前视图

系统只允许用户获取下一个视图的UIBackBarButtonItem,不允许获取当前视图的UIBackBarButtonItem(无论处于哪个视图都是这种说法)

self.navigationItem.backBarButtonItem的默认值为nil,除非用户对其赋值,否则无论在当前视图控制器(ViewController)的哪个生命周期中,self.navigationItem.backBarButtonItem永远为nil;

③下一个视图B的返回按钮由全局的NavigationController负责生成(它设置ViewController.navigationItem.backBarButtonItem属性),生成时机(依本人理解)为当前视图控制器A已经从NavigationController出栈而视图控制器B入栈时,此时视图控制器B正处于初始化阶段,控制器包含的View尚未显示在界面);

由全局的NavigationController负责生成的证据:苹果开发文档搜索关键词“UINavigationController”,可以看到里面写道:

In addition,the navigation controller object builds the contents of the navigation bar dynamically using the navigation items(instances of the UINavigationItem class)associated with the view controllers on the navigation stack.

④系统只允许用户定义返回UIBackBarButtonItem(也即是下个视图的返回按钮)的标题(title)和背景


定义背景的方法,提供两个本人觉得有参考价值的网页(未经本人验证正确性):

自定义iOS7导航栏背景,标题和返回按钮文字颜色

使用图片方式自定义iOS导航栏navigationItem的backBarButtonItem

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS 导航条是 iOS 应用程序中常用的一种导航控件,通常用于在不同的视图控制器之间进行导航。下面举例说明: 1. 在 iOS 开发中,可以通过代码或者 storyboard 对导航条进行配置和使用。 2. 在 storyboard 中,可以通过拖拽导航控制器和视图控制器之间的关系,来创建导航条。 3. 通过代码创建导航条的示例代码: ``` // 创建导航条 UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)]; // 创建导航项 UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:@"Title"]; // 添加导航项 [navigationBar pushNavigationItem:navigationItem animated:NO]; // 添加导航条到视图中 [self.view addSubview:navigationBar]; ``` 4. 通过代码设置导航条的标题和按钮: ``` // 设置导航条标题 navigationItem.title = @"Title"; // 创建导航条左侧按钮 UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(backAction)]; // 设置导航条左侧按钮 navigationItem.leftBarButtonItem = leftBarButtonItem; // 创建导航条右侧按钮 UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStylePlain target:self action:@selector(nextAction)]; // 设置导航条右侧按钮 navigationItem.rightBarButtonItem = rightBarButtonItem; ``` 这些代码可以帮助你快速创建和使用 iOS 导航条。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值