美文网首页iOS
UITabBarController 如何更换里面的viewco

UITabBarController 如何更换里面的viewco

作者: _会飞的鱼 | 来源:发表于2015-10-24 16:42 被阅读4056次

最近遇到个需求
游客模式下 先加载好UITabBarcontroller
然后登录之后 要根据 服务器返回的一个字段来 判断 改变TabBarcontroller里面的控制器
比方说 i == 0 tabbarcontroller 结构就不变
i ==1 tabbarcontroller 第三个TabBar 就移除当前的控制器
就换成另外的控制器

我总结了2种解决方案 (个人感觉只能将就用用)如果有更好的实现方式 欢迎@我

第一种方式 更换UINavigationController里面的UIViewController

//得到第3个需要变的navi控制器
HBBaseNavigationController *navi = [self.viewControllers objectAtIndex:2];
HBMyAssetsSlideViewController *assets = [[HBMyAssetsSlideViewController alloc] init];
[navi setViewControllers:@[assets] animated:NO];

得到的结果这样

CF16EB5B-8266-4F92-9A1C-452BB286492A.png

];
修改代码如下

//得到第3个需要变的navi控制器
HBBaseNavigationController *navi = [self.viewControllers objectAtIndex:2];

HBMyAssetsSlideViewController *assets = [[HBMyAssetsSlideViewController alloc] init];

//重新设置更换的视图控制器的属性(感觉多余 可是不写被替换的就空白)
[self addChildVcItem:assets title:@"资产" image:@"TabBar5" selectedImage:@"TabBar5Sel"];
[navi setViewControllers:@[assets] animated:NO];

//重新设置了一下他的标题和TabBar内容 
- (void)addChildVcItem:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
// 设置子控制器的文字
childVc.title = title; // 同时设置tabbar和navigationBar的文字
childVc.tabBarItem.title = title;
// 设置子控制器的图片
childVc.tabBarItem.image = [UIImage imageNamed:image];
//声明显示图片的原始式样 不要渲染
childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}

第二种方式 重新布局viewcontrollers

HBMyAssetsSlideViewController *assets = [[HBMyAssetsSlideViewController alloc] init];
HBBaseNavigationController *nav = [[HBBaseNavigationController alloc] initWithRootViewController:assets];
[self addChildVcItem:assets title:@"资产" image:@"TabBar5" selectedImage:@"TabBar5Sel"];
[self setViewControllers:@[[self.viewControllers objectAtIndex:0],[self.viewControllers objectAtIndex:1],nav,[self.viewControllers objectAtIndex:3]] animated:NO];

相关文章

网友评论

  • 对酒当歌的夜:我是在打开app加载一个,自动登录符合要求的话换tabbar,看似没问题,但是第一个VC被创建两次,数据又是异步加载的,有时候可以,有时候数据直接混乱,还在寻求更好的办法
    对酒当歌的夜:感谢楼主,用了你第二种方式直接创建好nav包含vc ,使用setViewControllers ,增加/减少/替换,都ok
  • 913c9536e19a:楼主现在有好的方法了嘛?第二种方式中:哪个HBBaseNavigationController是什么?
    _会飞的鱼:@小牛闯江湖 根的导航控制器 自定义的
  • 川农鉴黄师:楼主试过直接更换tabbar没?

本文标题:UITabBarController 如何更换里面的viewco

本文链接:https://www.haomeiwen.com/subject/xrothttx.html