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


相关推荐

  • int8与int16_将int型转为char型

    int8与int16_将int型转为char型工作中经常碰到int8_t、int16_t、int32_t、int64_t、uint8_t、size_t、ssize_t等数据类型,所以有必要对此进行梳理。int_t同类int_t为一个结构的标注,可以理解为type/typedef的缩写,表示它是通过typedef定义的,而不是一种新的数据类型。因为跨平台,不同的平台会有不同的字长,所以利用预编译和typedef可以最有效的维护代码。…

    2022年8月15日
    2
  • squirrel校园二手交易平台

    squirrel校园二手交易平台squirrel校园二手交易平台(适合寻找SSM项目练手的你。)项目起源:  期末的课程设计,三人一组,我和两个舍友,一时起兴,决定做一个校园二手交易平台,一开始兴致与激情满满,可是后期时间上的冲突,让我们三个人对于这个项目有点累。  这个项目刚定题两天,BezosLee由于通过了360的网上在线笔试,收到了360公司的免费培训邀请,要去北京的360总部进行培训一周,值得庆…

    2022年6月15日
    23
  • Trie 树内存消耗问题

    Trie 树内存消耗问题

    2021年5月25日
    127
  • 面试基础知识整理

    面试基础知识整理写在前面:3月伊始便经历了几次笔面试,深深感到自己知识储备的不足,痛下决心,在期末考试前一周,着手整理基础知识,希望可以对接下来的笔面试以及秋招有所帮助。本系列文章涉及数据结构及算法均使用Java语言描述。本文系基础知识整理,文章内容多来源于各经典书籍。本系列文章不定期更新,希望我可以有毅力完成这一工作。1.数据结构数组链表栈队列数图堆2.

    2022年6月20日
    25
  • docker开放2375端口号

    docker开放2375端口号1.编辑docker.service文件忘记编辑方法的老铁,可参考:https://blog.csdn.net/leinminna/article/details/97103557具体命令如下:vim/usr/lib/systemd/system/docker.service在ExecStart=/usr/bin/dockerd后插入-Htcp://0.0.0.0:2375-…

    2022年6月7日
    68
  • Log4j.properties配置文件详解「建议收藏」

    Log4j.properties配置文件详解「建议收藏」Log4J的配置文件(ConfigurationFile)就是用来设置记录器的级别、存放器和布局的,它可接key=value格式的设置或xml格式的设置信息。通过配置,可以创建出Log4J的运行环境。1.配置文件Log4J配置文件的基本格式如下:#配置根Loggerlog4j.rootLogger=[level],appenderName,appenderName1,…#配置日志信息输出目的地Appender及Appender选项log4j.a

    2022年9月30日
    0

发表回复

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

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