iOS开发 -- 自定义UITabBarController,子控件不隐藏问题

    相信各位同学有自定义UITabBarController的经验,比如在tabBar加一个Button,实现点击发布功能,炫一点的还能加AwesomeMenu(大概就是点击后弹出几个选项框框,如图)。


问题来了,就是push其他ViewController时,本来设置了

ViewController.hidesBottomBarWhenPushed = YES

只有本身tabBar隐藏,UITabBarController上面的子控件(如UIButton、AwesomeMenu)并没有隐藏。


解决:

一、如果在UITabBarController中添加的子控件是普通的UIButton,可以在自定义的controller中,把要添加的button作为tabBar的子视图,不要设为controller.view的子视图。

//创建一个<span style="font-family: Arial, Helvetica, sans-serif;">myTabBarController</span>继承自<span style="font-family: Arial, Helvetica, sans-serif;">UITabBarController</span>

 myTabBarController:UITabBarController
//在viewDidLayoutSubviews方法中添加子控件,为何要在这个方法中添加?因为每次从别的视图回到主视图,myTabBarController会重新刷新所有的子视图 如果没有重复添加子控件,就不显示了。故在此方法中添加。
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 17px; line-height: normal; font-family: Menlo;">- (<span style="font-size: 18px; line-height: normal; color: rgb(195, 34, 117);">void</span>)viewDidLayoutSubviews</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 17px; line-height: normal; font-family: Menlo;">{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 17px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">    [</span><span style="font-size: 18px; line-height: normal; color: rgb(195, 34, 117);">super</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span>viewDidLayoutSubviews<span style="font-variant-ligatures: no-common-ligatures; color: #000000">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 17px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">    [self.tabBar addSubView:myButton];</span></p><div><span style="font-variant-ligatures: no-common-ligatures; color: #000000">    myButton.center = self.tabBar.center;<span style="white-space:pre">	</span></span></div>}
这样,myButton就会跟着tabBar隐藏。


二、如果自定义的myTabBarController要添加的是AwesomeMenu,用了上面的方法,是不可行的。原因:

扫描二维码关注公众号,回复: 898532 查看本文章

UITabBarController的组织架构包括tabBar 、viewControllers(NSArray *)

  底部tabBar 与 占屏幕大部分的各个ViewController不在一个系统。

所以,用的是self.view添加子控件

<p style="margin-top: 0px; margin-bottom: 0px; font-size: 17px; line-height: normal; font-family: Menlo;">- (<span style="font-size: 18px; line-height: normal; color: rgb(195, 34, 117);">void</span>)viewDidLayoutSubviews</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 17px; line-height: normal; font-family: Menlo;">{</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 17px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><span style="color: rgb(0, 0, 0);">    [</span><span style="font-size: 18px; line-height: normal; color: rgb(195, 34, 117);">super</span><span style="color: rgb(0, 0, 0);"> </span>viewDidLayoutSubviews<span style="color: rgb(0, 0, 0);">];</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 17px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><span style="color: rgb(0, 0, 0);">    [self.view addSubView:myButton];</span></p><div>    myButton.hidden = NO;</div>}
在push时把hidden置1

self.tabBarController.myButton.hidden = YES;


这样在push的页面pop出来时,myButton稍微有点延迟,但影响不大。不介意的话可以用这个方法,我暂时没找到别的方法了。

猜你喜欢

转载自blog.csdn.net/u011494083/article/details/52170844