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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • vue项目打包步骤

    vue项目打包步骤vue项目打包终端运行命令npmrunbuild打包成功的标志与项目的改变,如下图:点击index.html,通过浏览器运行,出现以下报错,如图:那么应该如下修改呢?具体步骤如下:1、查看package.js文件的scripts命令2、打开webpack.dev.conf.js文件,找到publicPath:config.dev.assetsPublicPath,按Ctrl点击,跳转到in…

    2022年6月10日
    131
  • SVM, Softmax损失函数[通俗易懂]

    SVM, Softmax损失函数[通俗易懂]版权声明:本文为博主原创文章,未经博主允许不得转载。目录(?)[+]Deeplearning在计算机视觉方面具有广泛的应用,包括图像分类、目标识别、语义分隔、生成图像描述等各个方面。本系列博客将分享自己在这些方面的学习和认识,如有问题,欢迎交流。在使用卷积神经网络进行分类任务时,往往使用以下几类损失函数:平方误差损失SVM损失s

    2022年6月26日
    30
  • Python的常用函数:append()

    Python的常用函数:append()1.描述append()函数用于在列表末尾添加新的对象。2.语法list.append(obj)其中:list:列表对象;obj:添加到列表末尾的对象。注意:append()函数无返回值,但是会修改原本的列表。3.示例list=[‘abc’,’Harden’,’python’,’Curry’,…

    2022年4月4日
    87
  • 使用Setup Factory安装包制作工具制作安装包

    使用Setup Factory安装包制作工具制作安装包在我们开发完软件后,除了极个别案例我们把整个目录复制给客户用外,我们一般都需要做成安装包,方便整个软件的部署操作,以安装包的部署操作可能简单的是复制文件,也可能包括一些注册表、数据库等额外的操作,不过

    2022年7月3日
    30
  • 区块链|CAP原理[通俗易懂]

    区块链|CAP原理[通俗易懂]分布式系统(distributedsystem)正变得越来越重要,大型网站几乎都是分布式的。分布式系统的最大难点,就是各个节点的状态如何同步。CAP定理是这方面的基本定理,也是理解分布式系统的起点。一、分布式系统的三个指标ConsistencyAvailabilityPartitiontolerance它们的第一个字母分别是C、A、P。EricBrewer说,这三个指标不可能同时做到。这个结论就叫做CAP定理。二、Partitiontolerance先看Parti

    2022年5月19日
    82
  • 硬核!用Mac Mini翻新了一台12年前的电脑

    硬核!用Mac Mini翻新了一台12年前的电脑本文转载自差评君有这么一位差友,不知道从哪儿加到了托尼的微信,都还没打招呼呢,上来就向我展现了他最近的硬核杰作。他将M1款的MacMini塞进了09年款27寸iMac,并把详细过程一股脑发给了我。尽管不清楚这样做有什么意义,但确实是做的很硬核,手法也相当专业,简单来说,他愣是把一台新电脑,塞进了一台老电脑。。。整理了一下他发过来的内容,大致弄清了这事儿的前因后果。把M1款的Macmini,塞进了09年iMac的想法,是他在看到了油管上有一位叫L…

    2022年5月30日
    88

发表回复

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

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