如何隐藏uitabbarcontroller
我的 UITabBarController
有问题。在我的应用程序中,我想隐藏它,但不使用 hidesBottomBarWhenPushed ,因为我想隐藏它,而不是在推送它时隐藏它。例如,我想在按下应用程序中的隐藏按钮时隐藏它。
我在谷歌上读了很多文章,但我不知道如何做到这一点。
I have a problem with UITabBarController
. In my application, I want to hide it but without using hidesBottomBarWhenPushed
because I want to hide it not when I pushed it. For Example, I want to hide it when I press a Hide button in my application.
I read many articles in google but I cant find out how I can do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
我从我的工作代码中粘贴此...您可以调用这些方法来隐藏和显示 tabbarcontroller....只需将 tabbarcontroller 实例传递给这些函数..
I am pasting this from my working code... you can call these methods to hide and show the tabbarcontroller.... just pass tabbarcontroller instance to these functions..
修改了 Setomidor 的答案,使其适用于横向、纵向和 iPad(320 和 480 值仅适用于 iPhone)。
还修改了代码以处理 iOS 6 中引入的 UIDevice 方向更改,并确保即使设备仰卧时也能正常工作。
Modified Setomidor's answer to work on both landscape, portrait, and iPad (the 320 and 480 values only work on iPhone).
Also modified the code to handle changes introduced in iOS 6 with UIDevice orientation change and ensure that it works properly even when the device is lying on its back.
在按钮的操作方法中:
In your action method for the button:
Saurahb 和 karlbecker_com 的解决方案很棒,但当视图包含 tableview 而选项卡栏动画备份时,它们可能会导致明显的弹出效果。我做了一些修改并将其组合成一个函数(作为 UITabBarController 上的一个类别)。它并不完全完美(延迟校正动画),但在表格中给出了良好的结果。
如果您喜欢动画块和类别,请尝试一下。方向和设备友好。
UITabBarController+ShowHideBar.m:
UITabBarController+ShowHideBar.h:
用法:
Saurahb and karlbecker_com's solutions are great, though they can cause an obvious popping effect when the view contains a tableview while the tab bar animates back up. I've made some modifications and combined it into a single function (as a category on UITabBarController). It's not completely perfect (delayed correction animation) but gives good results with tables.
If you like animation blocks and categories, give this a try. Orientation and device friendly.
UITabBarController+ShowHideBar.m:
UITabBarController+ShowHideBar.h:
Usage:
Saurabh 的上述答案可以扩展为也适用于横向:
`
showTabBar() 对应的 x_pos 编号是
431
和271
。Saurabh's answer above can be extended to also work in landscape orientation:
`
The corresponding x_pos numbers for showTabBar() are
431
and271
.@karlbecker_com 答案非常适合 iPhone 4 和 iPhone 5。如果有人遇到 iOS7 底部黑条问题,请将 tabBarController 设置为半透明
@karlbecker_com Answer works perfect for both the iPhone 4 and iPhone 5. If anyone is having issues with iOS7 black bar at the bottom set the tabBarController to translucent
这是 karlbecker_com 的答案,移植到 MonoTouch (Xamarin.iOS)。
唯一的区别是我在继承自 UITabBarController 的类上实现了这些方法,因此对“
tabbarcontroller
”的引用被“this
”替换。This is karlbecker_com's answer, ported to MonoTouch (Xamarin.iOS).
The only difference is that I implemented the methods on a class that inherits from UITabBarController, so references to "
tabbarcontroller
" were replaced by "this
".在 iOS8 中,只需设置
tabBar
的hidden
属性就足够了就像在 Swift 中一样,我可以
在
appdelegate
中的didFinishLaunchingWithOptions
中执行此操作,并且效果很好,我想如果我没记错的话,在旧版 iOS 版本中您还需要设置将tabBar
的frame
移动到屏幕之外的位置,否则tabbar
将不会显示,但仍会占用空间。in iOS8 it is enough to just set the
hidden
property of thetabBar
Like in Swift you can
I do this in my
didFinishLaunchingWithOptions
in theappdelegate
and it works fine, I think if I remember correctly in the older iOS versions you also needed to set theframe
of thetabBar
to something outside the screen, otherwise thetabbar
would not show but it will still occupy the space.从 IOS 7.1 开始,“Swift” 解决方案:
希望这能有所帮助!
Since IOS 7.1, "Swift" solutions:
Hope this could help!
您可以推送模式视图控制器,
这将在当前视图之上创建一个全新的全屏视图。
使用
dismissModalViewController:animated:
关闭 istyou can push a modal view controller
this will create a completely new view fullscreen above your current one.
dismiss ist with
dismissModalViewController:animated:
在完全相同的用例中,下面的解决方案对我来说效果很好,我必须使用 TabBar 动画移动到全屏模式。
基本上,这个想法是
制作UITabBar的快照;
将快照的UIImage添加到与UITabBar同框的UIImageView中;
调整底层视图的大小并将其放置在self.tabBarController.view上;
将UITabBar的alpha设置为0.0;
将 UIImageView 与 UITabBar 的快照放在 self.tabBarController.view 上;
一旦实现上述目标,就可以制作任何类型的动画
The solution below works fine for me in exactly the same use case where I have to move to fullscreen mode with TabBar animation.
Basically, the idea is
to make a snapshot of UITabBar;
add the UIImage of the snapshot to UIImageView which has the same frame as UITabBar does;
resize underlying view and place it on self.tabBarController.view;
set UITabBar's alpha to be 0.0;
place the UIImageView with UITabBar's snapshot on the self.tabBarController.view;
Once the above is achieved, do any kind of animation
我几乎尝试了所有这些答案,但没有一个对我有用。我的应用程序有一个 UITabBarController 作为根视图,每个选项卡都有一个 UINavigationController。其中一个 UINavigationController 有一个 UICollectionViewController 作为顶视图控制器。当用户在 UICollectionView 中选择一个项目时,我希望将详细视图控制器推送到导航堆栈上。我的详细视图底部有一个工具栏。我不希望工具栏出现在选项卡栏的顶部,因为这看起来很愚蠢,并且在此视图中不需要切换选项卡上下文。我可以通过手动放置 UIToolbars 和 UITabBars 而不是使用 UITabBarController 和内置 UIToolbar 来轻松解决这个问题,但这看起来重构太多而且有点不优雅。
最后我的解决方案相当简单:将 UITabBarController 的边界扩展到屏幕底部。我将其添加到我的详细视图控制器中:
然后,为了在用户弹回 UINavigationController 的顶部时重新显示选项卡栏,我将其添加到我的顶部视图控制器中:
I tried pretty much all these answers but none of them worked for me. My app has a UITabBarController as the root view, and each tab has a UINavigationController. One of the UINavigationControllers has a UICollectionViewController as the top view controller. When the user selects an item in the UICollectionView, I wanted the detail view controller to get pushed onto the navigation stack. My detail view then had a toolbar at the bottom. I didn't want the toolbar to appear on top of the tab bar as that looks goofy, and switching tab contexts won't be needed from this view. I could probably have easily solved this by manually placing UIToolbars and UITabBars and not using UITabBarController and the built-in UIToolbar, but that seemed like too much refactoring and a bit inelegant.
In the end my solution was fairly simple: extend the bounds of the UITabBarController off the bottom of the screen. I added this to my detail view controller:
Then to reshow the tab bar when the user pops back to the top of my UINavigationController, I added this to my top view controller:
@Saurabh 代码的 Swift 和修改版本
方法
显示
隐藏
Swift and modified version of @Saurabh code
Method
To show
To hide
带动画的swift版本,需要自己设置一个属性
isHideTabBar
。A swift version with animation, you need set a property
isHideTabBar
by yourself.更新并适用于 swift 5 和 ios 14.0
这是一个更直接的端口(未经测试):
Updated and Working for swift 5 and ios 14.0
This is a more direct port (not tested):
隐藏选项卡栏不是一个足够的解决方案,它不会调整当前视图控制器的视图高度。
相反,您可以简单地转换选项卡栏本身,无论是通过其高度(隐藏)还是身份转换以重置为可见。
请注意,您可能需要将视图控制器设置为“延伸到底部栏下方”和“延伸到不透明栏下方”,以在动画期间删除黑色背景。
Hiding the tab bar is not an adequate solution, it won't adjust the current view controllers view height.
Instead you may simply transform the tab bar itself, either by it's height (to hide) or an identity transform to reset to visible.
Note that you may need to set your view controller to 'extends below bottom bars' and 'extends under opaque bars' to remove the black background during animation.