这里指的不是跳转页面之后隐藏tabbar,如果是跳转后隐藏直接hidebarButtonItem = yes就搞定,这里是本页面隐藏之后在原tabbar位置新建一个view,并在view上添加按钮,但是接受不到点击事件,在网上看了很多人都遇到这个问题,但是解释的都不够,后来自己摸索了一个方法,拿来分享,欢迎指正。
首先就是两个方法,隐藏和显示tabbar
//隐藏tabbar
- (void)hideTabBar
{
if (self.tabBarController.tabBar.hidden == YES)
{
return;
}
UIView *contentView;
if ( [[self.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]] )
contentView = [self.tabBarController.view.subviews objectAtIndex:1];
else
contentView = [self.tabBarController.view.subviews objectAtIndex:0];
contentView.frame = CGRectMake(contentView.bounds.origin.x, contentView.bounds.origin.y, contentView.bounds.size.width, contentView.bounds.size.height + self.tabBarController.tabBar.frame.size.height);
self.tabBarController.tabBar.hidden = YES;
}
//显示tabbar
- (void)showTabBar
{
if (self.tabBarController.tabBar.hidden == NO)
{
return;
}
UIView *contentView;
if ([[self.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]])
contentView = [self.tabBarController.view.subviews objectAtIndex:1];
else
contentView = [self.tabBarController.view.subviews objectAtIndex:0];
contentView.frame = CGRectMake(contentView.bounds.origin.x, contentView.bounds.origin.y, contentView.bounds.size.width, contentView.bounds.size.height - self.tabBarController.tabBar.frame.size.height);
self.tabBarController.tabBar.hidden = NO;
}
在你需要调用的地方调用这两个函数就可以了,然而重点并不在这里,这些代码百度到处都是,重点是,当你隐藏之后在原位置添加一个VIew但是接受不到点击事件,一开始遇到这个问题我认为是View的交互事件没有打开,因为新建的View可以显示,并没有被遮住,说这么多,一句最简单的代码搞定问题
[self.tabBarController.view addSubview:viewForEidit];
其中viewForEdit是我自己新建的view,初始化过程等就不赘述了。尝试很多方法后,之前一直是把viewForEdit加在self.view上,所以一直无果。这里面的机制我也没有搞清楚,但是就是可以实现。希望大家明白深入一点的可以多指教。
541370008@qq.com