自定义UITabBarController以及UITabBar的分析

最近在研究一些iOS中的基本问题,发现网上的答案很多,但是讲的都不够清晰,自己结合使用的实际情况总结下。

很多时候我们需要自定义UITabBarController或者UITabBar,但是往往用起来有些地方让人困惑。

1、先说下网络上常见的方式:隐藏原有的TabBar,使用UIView完全重新定义TabBar,这样的好处就是,定制化的程度很高

而且很多东西可以完全推倒重来,但是有一个很严重的问题,当使用

viewController.hidesBottomBarWhenPushed = YES的时候,还是无法隐藏UITabBar,这个始终无法隐藏,除非采用自定制的动画移开这个UIView或者设置

这个为Hidden, 最重要的问题也在这里, 所以采用隐藏原有的TabBar的方式之后很多人抱怨无法通过viewController.hidesBottomBarWhenPushed = YES

来隐藏TabBar,网上有很多类似的代码,我这里就不贴源代码了,问题已经分析出来了。

网易新闻的iOS客户端就是这样的方式实现的,有一点点小的瑕疵

还有一种类型的TabBar可以使用这样的方式来实现,上截图

这个就完全是重写了TabBar,而且和以前的系统原生的没有任何关系了,但是用的不多。

 

 

2、使用自定义UITabBarController, 继承UITabBarController,采用两种方式(为了适应iOS4 和 iOS5)

主要代码如下

//方法一

        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabbar_background"]];

        imageView.frame = CGRectMake(0, 0, self.tabBar.frame.size.width, self.tabBar.frame.size.height);

        NSLog(@"the frame is %f---%f", self.tabBar.frame.size.width, self.tabBar.frame.size.height);

        imageView.contentMode = UIViewContentModeScaleToFill;

        [[self tabBar] insertSubview:imageView atIndex:0];

        [imageView release];

        

        //方法二  适用于iOS5+

//        [self.tabBar setBackgroundImage:[UIImage imageNamed:@"tabbar_background"]];

方法一只能适用于iOS5以下的系统,在iOS5+上面无法使用,所以必须把这两种方法结合起来

当使用viewController.hidesBottomBarWhenPushed = YES

然后再加上

[self.navigationControllerpushViewController:ucenter animated:YES];

就可以实现UITabBar的隐藏,同时也实现了自定义的UITabBarController

截图如下,新浪微博的客户端就是采用这样的方式实现的。

push之后的图片

 

 

原文地址:https://www.cnblogs.com/easonoutlook/p/2726133.html