修改navigationItem.backBarButtonItem.title的值无效的解决办法

iOS导航返回按钮定制

http://www.cnblogs.com/weilaikeji/archive/2013/02/17/2913842.html

在iphone开发中,设置navigationController中返回按钮的标题,默认为前一个视图中标题的title(请注意,必须是前一个视图控制器!!!backBarButtonItem属性是对应于本视图控制器的下一级控制器的返回按钮,区别于leftBarButtonItem)。

如果设置,在前一个视图中写下:

20


  1. UIBarButtonItem *temporaryBarButtonItem=[[UIBarButtonItem alloc] init];  
  2. temporaryBarButtonItem.title=@"Back";  
  3. self.navigationItem.backBarButtonItem = temporaryBarButtonItem;  
  4. [temporaryBarButtonItem release]; 

为何直接设置

   

backBarButtonItem的title无效呢?

查看苹果文档UIBarButtonItem的父类UIBarItem的title属性描述:

  You should set this property before adding the item to a bar. The default value is nil.而  

backBarButtonItem原本就内置,故无法修改其title,只能重置这个控件本身。

针对这种情况,解决办法如下:

在第一级页面的viewDidLoad方法中加入以下代码:  UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
  temporaryBarButtonItem.title =@"返回";
  self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
  [temporaryBarButtonItem release];  
 也就是用一个新的按钮在进行导航前将原来的返回按钮替换掉就可以了。

 


讲一下这段逻辑 private func updateNavigationBarButtons() { if !appContext.isLogin { //未登录 if showLocalDeviceOnly { if localDeviceCount == 0 { self.navigationItem.rightBarButtonItems = [addButtonmItem] } else { self.navigationItem.rightBarButtonItems = [addButtonmItem, negativeButtonItem] } moreSiteButton.isHidden = localDeviceCount == 0 tabBarCollectionView.isHidden = localDeviceCount == 0 } else { //如果未登录则左边按钮不显示 self.navigationItem.leftBarButtonItems = [mineButtonItem] //右边显示所有按钮 self.navigationItem.rightBarButtonItems = [addButtonmItem,negativeButtonItem] tabBarCollectionView.isHidden = true } scrollView.setContentOffset(CGPoint(x: 0, y: 62), animated: false) } else { //如果登录了则左边按钮显示 if showLocalDeviceOnly { moreSiteButton.isHidden = localDeviceCount == 0 tabBarCollectionView.isHidden = localDeviceCount == 0 if localDeviceCount == 0 { self.navigationItem.rightBarButtonItems = [addButtonmItem] } else { self.navigationItem.rightBarButtonItems = [addButtonmItem, negativeButtonItem, searchItem] } removeLeftBarButtonItem() addCustomNavigationLeftBarButtonItem(title: nil, image: TPImageLiteral("common_light_back_nor")) } else { moreSiteButton.isHidden = false tabBarCollectionView.isHidden = false if appContext.isVmsLogin { // 登录状态VMS if isDeviceListOld { removeLeftBarButtonItem() addCustomNavigationLeftBarButtonItem(title: nil, image: TPImageLiteral("common_light_back_nor")) self.navigationItem.rightBarButtonItems = [searchItem, changeDisplayTypeItem] } else { // tab新版VMS不能滚动 tabBarCollectionView.isScrollEnabled = true // addLeftBarButtonItem() if appContext.loginType == .cloud { self.navigationItem.rightBarButtonItems = [addButtonmItem, negativeButtonItem] } else { self.navigationItem.rightBarButtonItems = [] } } } else { // 登录状态个人版 reqVmsOrgListCode = appContext.requestVMSOrganizationList(withUsername: appContext.accountUsername, password: appContext.accountPassword ?? "") // removeLeftBarButtonItem() // if let hasVms = UserDefaults.standard.object(forKey: kHasCloudVMSOrganization) as? Bool, hasVms { // addLeftBarButtonItem() // } self.navigationItem.rightBarButtonItems = [addButtonmItem, negativeButtonItem] } } scrollView.setContentOffset(CGPoint(x: 0, y: 62), animated: false) } }
11-28
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值