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


相关推荐

  • visifire 使用备忘

    visifire 使用备忘//图表绑定辅助类publicclassChartValue:INotifyPropertyChanged{publicChartValue(){}publicChartValue(stringxlabel,decimalvalue){Label=xlabel;_yValue=value;}de…

    2022年7月21日
    9
  • 云服务是免费的吗_云服务器收费

    云服务是免费的吗_云服务器收费近年来,云服务器的普及率快速上升,相当一部分企业从传统服务器转向云服务器,而随着市场的发展,云服务器供应商尤其多,服务器供应商竞争日趋激烈。此时不少服务商表示自己推出永久免费使用的云服务器,面对这样的消息不少企业会感到疑惑,永久免费使用的云服务器究竟是否可信?那么下面就由摩杜云小杜和大家讲一讲有没有永久免费的云服务器。一、首先市场上根本就没有所谓的永久免费使用的云服务器虽然现如今云技术发展快速,但是云资源的成本还是很高的,所以商家为了自己获益,不可能会提供免费的云主机租用服务。但是目前市场上有服务商提供

    2022年10月6日
    0
  • macbookpro双系统分区_macbook双系统分区调整

    macbookpro双系统分区_macbook双系统分区调整一些前序知识:1、Mac系统不是Intel的i386架构,没有Bios,但是有EFI,通过EFI管理系统的引导。2、Mac系统的分区表采用GUID,不是MBR。因此如果硬盘完全交给windows控制,会导致Mac系统无法启动。3、任何对MBR的强行操作,会导致已经装好的双系统引导失效。4、Mac的EFI分区会用GPT锁定,不要尝试去操作这个分区。我们…

    2022年10月5日
    0
  • kafka删除topic中的数据_kafka删除数据

    kafka删除topic中的数据_kafka删除数据删除topic里面的数据这里没有单独的清空数据的命令,这里要达到清空数据的目的只需要以下步骤:一、如果当前topic没有使用过即没有传输过信息:可以彻底删除。二、如果当前topic有使用过即有过传输过信息:并没有真正删除topic只是把这个topic标记为删除(markedfordeletion)。想要彻底删除topic数据要经过下面两个步骤:①:删除topic,重新用创建to…

    2022年10月16日
    0
  • bfs官网_山谷和山脉

    bfs官网_山谷和山脉FGD小朋友特别喜欢爬山,在爬山的时候他就在研究山峰和山谷。为了能够对旅程有一个安排,他想知道山峰和山谷的数量。给定一个地图,为FGD想要旅行的区域,地图被分为 n×n 的网格,每个格子 (i,j) 的高度 w(i,j) 是给定的。若两个格子有公共顶点,那么它们就是相邻的格子,如与 (i,j) 相邻的格子有(i−1,j−1),(i−1,j),(i−1,j+1),(i,j−1),(i,j+1),(i+1,j−1),(i+1,j),(i+1,j+1)。我们定义一个格子的集合 S 为山峰(山谷)当且仅当:

    2022年8月9日
    4
  • settimeout()停止_需求方案

    settimeout()停止_需求方案转载https://aotu.io/notes/2017/09/25/manage-setTimeout-an-setInterval/在管理setTimeout&amp;setInterval这两个APIs时,笔者通常会在顶级(全局)作用域创建一个叫 timer 的对象,在它下面有两个数组成员——{sto,siv},用它们来分别存储需要管理的setTimeoutID/…

    2022年10月3日
    0

发表回复

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

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