自定义标签栏问题
Posted
技术标签:
【中文标题】自定义标签栏问题【英文标题】:Custom Tabbar issue 【发布时间】:2012-10-09 06:56:15 【问题描述】:我的应用程序包含标签栏和状态栏。标签栏的构造类似于带有背景图像的按钮,并在标签栏上添加了此按钮。问题是,按钮的高度为 47。但我们的默认标签栏的高度为 49。所以标签栏和视图之间存在间隙。在 xib 中,我试图更改视图的框架大小。但这是不可能的。默认情况下它会出现 411。我该如何解决这个问题?有没有办法创建自定义标签栏?
这是我的标签栏代码
viewController1 *viewController1Obj = [[viewController1 alloc] initWithNibName:@"viewController1" bundle:nil];
viewController2 *viewController2Obj = [[viewController2 alloc] initWithNibName:@"viewController2" bundle:nil];
viewController3 *viewController3Obj = [[viewController3 alloc] initWithNibName:@"viewController3" bundle:nil];
viewController4 *viewController4Obj = [[viewController4 alloc] initWithNibName:@"viewController4" bundle:nil];
viewController5 *viewController5Obj = [[viewController5 alloc] initWithNibName:@"viewController5" bundle:nil];
UINavigationController *nav1=[[UINavigationController alloc]initWithRootViewController:viewController1Obj];
UINavigationController *nav2=[[UINavigationController alloc]initWithRootViewController:viewController2Obj];
UINavigationController *nav3=[[UINavigationController alloc]initWithRootViewController:viewController3Obj];
UINavigationController *nav4=[[UINavigationController alloc]initWithRootViewController:viewController4Obj];
UINavigationController *nav5=[[UINavigationController alloc]initWithRootViewController:viewController5Obj];
nav1.navigationBarHidden=YES;
nav2.navigationBarHidden=YES;
nav3.navigationBarHidden=YES;
nav4.navigationBarHidden=YES;
nav5.navigationBarHidden=YES;
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:nav1, nav2,nav3,nav4 ,nav5, nil];
self.tabBarController.tabBar.frame = CGRectMake(0.0f, self.tabBarController.tabBar.frame.origin.y, 320.0f, 47.0f);
[self.window addSubview:self.tabBarController.view];
self.tabBarController.view.backgroundColor = [UIColor redColor];
self.tabBtn1 = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 64.0f, 47.0f)];
[self.tabBtn1 setImage:[UIImage imageNamed:@"button1.png"] forState:0];
[self.tabBtn1 setTag:77777];
self.tabBtn1.adjustsImageWhenHighlighted = NO;
[self.tabBtn1 addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
self.tabBtn2 = [[UIButton alloc] initWithFrame:CGRectMake(64.0f, 0.0f, 64.0f, 47.0f)];
[self.tabBtn2 setImage:[UIImage imageNamed:@"button2.png"] forState:0];
[self.tabBtn2 setTag:77778];
self.tabBtn2.adjustsImageWhenHighlighted = NO;
[self.tabBtn2 addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
self.tabBtn3 = [[UIButton alloc] initWithFrame:CGRectMake(128.0f, 0.0f, 64.0f, 47.0f)];
[self.tabBtn3 setImage:[UIImage imageNamed:@"button3.png"] forState:0];
[self.tabBtn3 setTag:77779];
self.tabBtn3.adjustsImageWhenHighlighted = NO;
[self.tabBtn3 addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
self.tabBtn4 = [[UIButton alloc] initWithFrame:CGRectMake(192.0f, 0.0f, 64.0f, 47.0f)];
[self.tabBtn4 setImage:[UIImage imageNamed:@"button4.png"] forState:0];
[self.tabBtn4 setTag:77780];
self.tabBtn4.adjustsImageWhenHighlighted = NO;
[self.tabBtn4 addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
self.tabBtn5 = [[UIButton alloc] initWithFrame:CGRectMake(256.0f, 0.0f, 64.0f, 47.0f)];
[self.tabBtn5 setImage:[UIImage imageNamed:@"button5.png"] forState:0];
[self.tabBtn5 setTag:77781];
self.tabBtn5.adjustsImageWhenHighlighted = NO;
[self.tabBtn5 addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.tabBarController.tabBar addSubview:self.tabBtn1];
[self.tabBarController.tabBar addSubview:self.tabBtn2];
[self.tabBarController.tabBar addSubview:self.tabBtn3];
[self.tabBarController.tabBar addSubview:self.tabBtn4];
[self.tabBarController.tabBar addSubview:self.tabBtn5];
在我得到的间隙中显示红色。
【问题讨论】:
【参考方案1】:是的,您可以创建自己的自定义标签栏。使用以下链接解决您的问题。
iPhone: How can I build my own TabBar?
ios custom tabbar
How could I create a custom tabbar similar to this
http://kurrytran.blogspot.in/2011/10/ios-5-tutorial-creating-custom-tab-bar.html
我给你一个链接,它有预先制作的自定义标签栏,可以在这里帮助你。
http://www.cocoacontrols.com/search?utf8=%E2%9C%93&q=tabbar
【讨论】:
【参考方案2】:我不确定你是如何创建标签栏的,但也许这个链接可以帮助你。 它将栏的高度设置为大于 49 的值
https://gist.github.com/869331
或者你也可以查看这个帖子
How could I create a custom tabbar similar to this
【讨论】:
【参考方案3】:我通过在 UITabbarController 子视图中设置 UITransitionView 的框架解决了这个问题
[[[self.tabBarController.view subviews]objectAtIndex:0] setFrame:CGRectMake(0, 0, 320, 521)];
现在差距已经过去了。
【讨论】:
以上是关于自定义标签栏问题的主要内容,如果未能解决你的问题,请参考以下文章