tabBar选项卡的背景色如何修改(自定义tabBar)&&推送nav到指定(根)视图控制器

tabBar选项卡的背景色如何修改(自定义tabBar)&&推送nav到指定(根)视图控制器

在tabBar区域加个视图

CGRect frame = CGRectMake(0032048);

UIView *v = [[UIView alloc]initWithFrame:frame];

[v setBackgroundColor:[[UIColor allor]initWithRed:70.0/255.0

green:65.0/255.0

blue:62.0/255.0

alpha:1.0]];

[rootController.tabBar insertSubview:v atIndex:0];

[v release];

注意:

addSubview是将view添加到所有层的最顶层,相当于将insertSubView的atIndex参数设置成[view.subviews count]。这是因为子视图是以栈的方式存放的,每次添加子视图都是在最后面追加。insertSubview是添加到指定的层位置。

[view addSubview:one view] 等价于[view insertSubview:oneview atIndex:[view.subviews count]];

 

 

自定义透明tabBar

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

// Override point for customization after application launch.
FirstViewController *mainViewController = [[FirstViewController alloc] init];
SecondViewController *searchViewController = [[SecondViewController alloc]init];
ThirdViewController *myselfViewController = [[ThirdViewController alloc]init];
ForthViewController *settingViewController = [[ForthViewController alloc]init];

//隐藏tabbar所留下的黑边(试着注释后你会知道这个的作用)
mainViewController.hidesBottomBarWhenPushed = true;
searchViewController.hidesBottomBarWhenPushed = true;
myselfViewController.hidesBottomBarWhenPushed = true;
settingViewController.hidesBottomBarWhenPushed = true;

mainViewController.title = @"首页";
searchViewController.title = @"搜索";
myselfViewController.title = @"";
settingViewController.title = @"设置";

//创建导航
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:mainViewController ];
UINavigationController *nav1 = [[ UINavigationController alloc] initWithRootViewController:searchViewController];
UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:myselfViewController];
UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:settingViewController];

//创建数组
NSMutableArray *controllers = [[NSMutableArray alloc]init];
[controllers addObject:nav];
[controllers addObject:nav1];
[controllers addObject:nav2];
[controllers addObject:nav3];

//创建tabbar
tabBarController = [[ CustomTabBar alloc] init];
tabBarController.viewControllers = controllers;
tabBarController.selectedIndex = 0;

//显示
[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];


return YES;
}

 

附:

改变tabbar图标的办法

tabBar选项卡的背景色如何修改(自定义tabBar)&&推送nav到指定(根)视图控制器

问题:我点击click me,会push到下一个页面,如果用的是系统的tabbar,单击首页的时候会pop到首页,但是用自定义的无法实现,求解决方案.

 

解决:自行替换下面的方法,其实也就是加了一句话
– (void)selectedTab:(UIButton *)button{

    if (self.currentSelectedIndex == button.tag) {

  //判断是否是UINavigation类

  if([[self.viewControllers objectAtIndex:button.tag]isKindOfClass:[UINavigationController class]])

   //返回根视图控制器
        [[self.viewControllers objectAtIndex:button.tag] popToRootViewControllerAnimated:YES];
    }
    self.currentSelectedIndex = button.tag;
    self.selectedIndex = self.currentSelectedIndex;
    [self performSelector:@selector(slideTabBg:) withObject:button];
}

#import "Ivan_UITabBar.h"


@implementation Ivan_UITabBar
@synthesize currentSelectedIndex;
@synthesize buttons;

- (void)viewDidAppear:(BOOL)animated{
slideBg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"slide"]];
[self hideRealTabBar];
[self customTabBar];
}

- (void)hideRealTabBar{
for(UIView *view in self.view.subviews){
if([view isKindOfClass:[UITabBar class]]){
view.hidden = YES;
break;
}
}
}

- (void)customTabBar{
UIView *tabBarBackGroundView = [[UIView alloc] initWithFrame:self.tabBar.frame];
tabBarBackGroundView.backgroundColor = [UIColor grayColor];
//创建按钮
int viewCount = self.viewControllers.count > 5 ? 5 : self.viewControllers.count;
self.buttons = [NSMutableArray arrayWithCapacity:viewCount];
double _width = 320 / viewCount;
// NSLog(@"%d",viewCount);
double _height = self.tabBar.frame.size.height;
for (int i = 0; i < viewCount; i++) {
UIViewController *v = [self.viewControllers objectAtIndex:i];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(i*_width, 0, _width, _height);
;
btn.tag = i;
// NSLog(@"%@",v.tabBarItem.image);
;
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, _height-20, _width, _height-30)];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.text = v.tabBarItem.title;
titleLabel.textAlignment = 1;
titleLabel.textColor = [UIColor whiteColor];
;
[titleLabel release];
[self.buttons addObject:btn];
[tabBarBackGroundView addSubview:btn];
}
[self.view addSubview:tabBarBackGroundView];
[tabBarBackGroundView addSubview:slideBg];
[self.view insertSubview:tabBarBackGroundView atIndex:0];
[tabBarBackGroundView release];
[self selectedTab:[self.buttons objectAtIndex:0]];
}

- (void)selectedTab:(UIButton *)button{
if (self.currentSelectedIndex == button.tag) {
[[self.viewControllers objectAtIndex:button.tag] popToRootViewControllerAnimated:YES];
}
self.currentSelectedIndex = button.tag;
self.selectedIndex = self.currentSelectedIndex;
[self performSelector:@selector(slideTabBg:) withObject:button];
}

- (void)slideTabBg:(UIButton *)btn{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.20];
[UIView setAnimationDelegate:self];
slideBg.frame = btn.frame;
[UIView commitAnimations];
}

 

UITabBarController注意的地方

如果你的程序界面如上图所示的这个样子,那么你一定是同时使用了UITabBarController以及UINavigationController吧。

再如果,你希望实现这样子的效果:

1。用户起初在“分类”这个视图里面查看了一些菜系,

2。然后他点击了“搜索”Tab,又在里面看了一些搜索出来的菜谱,

3。再然后当他准备回到“分类”这个视图的时候,事实上用户心里是希望回到“分类”这个视图的根视图,在那里重新选择分类,进入分类再看菜谱。但是iPhone默认是记忆用户之前在第一步里面的那个视图的,不会自动回到根视图。

解决办法是将这个

– (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {

viewController popToRootViewControllerAnimated:YES];

}

添加到AppDelegate.m文件中。一般人都是这么做的。

 

但是!

 

其实在这里隐藏着一个问题,如果如图所示你的4个TabBar都是指向4个NavigationController,那么没有问题,运行OK。

但如果你的4个TabBar有任何一个指向的不是NavigationController,那么程序就会crash。因为非NavigationController不能够响应 popToRootViewControllerAnimated: 方法。

我的第4个TabBar指向的是一个ViewController,程序运行之后一点击第4个TabBar就崩溃,自己也只是在ViewController和IB里面仔细找错误,浪费了很多时间。

 

下面贴出更加安全的方法,把上面的那段代码改成这样子:

 

– (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {

if ([viewController isKindOfClass:[UINavigationController class]]) {

[(UINavigationController *)viewController popToRootViewControllerAnimated:YES];

}

}

就好了。

 

//推送到指定视图控制器

XXView *rootViewController = nil;
  for (UIViewController *VC in self.navigationController.viewControllers)

  {

    if ([VC isKindOfClass:[XXView class]]) {

    rootViewController = (XXView *)VC;
  }
}
[self.navigationController popToViewController:rootViewController animated:YES];

转载于:https://www.cnblogs.com/pengyingh/articles/2381730.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/110464.html原文链接:https://javaforall.net

(0)
上一篇 2021年8月17日 上午9:00
下一篇 2021年8月17日 上午10:00


相关推荐

  • 一个细菌基因组完整分析脚本

    一个细菌基因组完整分析脚本本文转自 基因学苑 已获授权分析流程图一 数据质控利用 fastqc 软件对原始测序 reads 进行指控 生成网页帮统计报告 根据报告内容对数据机型过滤 mkdirresultf

    2026年3月18日
    2
  • SQL SERVER DATEADD()函数说明

    SQL SERVER DATEADD()函数说明适用场景 DATEADD 函数在日期中添加或减去指定的时间间隔 语法格式 DATEADD datepart number date 字段说明 date 参数是合法的日期表达式 number 是您希望添加的间隔数 可使用的 datepart 参数值 datepart 缩写 年 yy yyyy 季度 q 月 mm m 年中的日 dy y 日 dd d 周 wk ww 星期 dw w 小时 hh

    2026年3月17日
    2
  • LMArena最新排名:文心大模型5.0文本能力国内第一

    LMArena最新排名:文心大模型5.0文本能力国内第一

    2026年3月12日
    1
  • c语言必备100代码(代码大全)

    一个完全入门初学者如何学代码,读代码和写代码,,我想学代码不知道方向谁能给我指明一个方向?1、学代码:前提是你的复有一个比较系统的学习.认真完成每一个课程中的案例.2、读代码:分制两步走:前期能读懂自己写的代码.2113后期能读懂他人写的代码和大致的知道底层的某些源码的含义.多去5261看开发文档(开发文档建议使用官方提供的4102英文版、不要使用中文自己害自己)3、写代码1653:前提是你要有…

    2022年4月18日
    1.2K
  • AspNetCore 中使用 Newlife.IdentityServer4 管理 IdentityServer 数据

    AspNetCore 中使用 Newlife.IdentityServer4 管理 IdentityServer 数据本文讲述如何实现 IdentityServ 数据的管理 包括添加删除等基础操作和相应 UI 后端此处直列关键点 不做详细描述 此项目跟普通的数据库管理无异 所以只做简单记录实例请查看这里添加引用新建 asp netcore 项目 IdentityServ XCode 引用 代码入口添加服务 视自己情况而定 options 的参数需要与前端配合添加 Easy Admin 引用 Easy Admin 包含了具有增删查改的基类控制器 并且封装了统一的响应结果 真实结果包含在 Data 属性

    2026年3月19日
    3
  • 如何使用 Nano Banana 2 创建专业 AI 图像:完整教程

    如何使用 Nano Banana 2 创建专业 AI 图像:完整教程

    2026年3月15日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号