hidesBottomBarWhenPushed使UITabBar变为“跳转”状态。 [英] hidesBottomBarWhenPushed makes UITabBar "jump"
问题描述
我有一个带有标签栏和导航栏的应用。
I have an app with a tabbar and a navbar.
我有一个 BaseVC
和一个 DetailVC
。我要从 BaseVC
推送 DetailVC
。我希望选项卡在推送的VC DetailVC
下。我正在使用 hidesBottomBarWhenPushed
来实现它。它的效果很好,但是出于某种原因,在动画制作过程中,标签栏仍然可见,而动画结束时,标签栏也被隐藏了。我也希望它也位于动画的推送VC下。
I have a BaseVC
and a DetailVC
. I'm pushing DetailVC
from BaseVC
. I want the tabbar to be under the pushed VC DetailVC
. I'm using hidesBottomBarWhenPushed
to achieve it. It works great, but for some reason while it's animating the push the tabbar is still visible and just when the animation ends the tabbar is hidden. I want it to be under the pushed VC in the animation too.
我的代码是:
self.hidesBottomBarWhenPushed = true
self.navigationController?.pushViewController(detailVC, animated: true)
self.hidesBottomBarWhenPushed = false
结果(错误)是这样的:
And the result (the bug) is this:
任何人都有知道为什么标签栏跳转吗?谢谢!
Anyone has an idea why the tabbar "jumps"? Thank you!
推荐答案
在研究了有问题的项目后,我找到了一种使其工作的方法:
Having looked at the project in question I have found one way to make it work:
- 从
TabBarViewController
删除viewWillLayoutSubviews
-
创建一个名为MyTabBar的新swift文件(或您想要的任何文件),然后放置
- Remove the
viewWillLayoutSubviews
from theTabBarViewController
so that it is not determining the height of the tab bar anymore and thus not stopping the animation working correctly. Create a new swift file called MyTabBar (or whatever you want) and put this in it:
import UIKit
class MyTabBar: UITabBar {
var tabBarHeight: CGFloat = 100
override func sizeThatFits(_ size: CGSize) -> CGSize {
let superSize = super.sizeThatFits(size)
return CGSize(width: superSize.width, height: self.tabBarHeight)
}
}
创建一个名为TabBarStoryboard的情节提要(或其他)。
Create a storyboard called TabBarStoryboard (or whatever). It's not going to be used for anything other then to hold a UITabBarController which you later create.
在情节提要中,将UITabBarController的类类型设置为您的类,然后再将其用于保存以后创建的UITabBarController。 TabBarViewController
的实例,这样它在实例化时将获得正确的类。
In the storyboard set the class type of the UITabBarController to your class of TabBarViewController
so it gets the correct class when instantiated.
在情节提要中设置类的类型属于UITabBarController的UITabBar属于MyTabBar,因此在实例化时也是正确的类。
In the storyboard set the class type of the UITabBar that belongs to the UITabBarController to MyTabBar so that it too is the correct class when instantiated.
在您的RootViewController中,将其替换为:
In your RootViewController replace this:
fileprivate let tabBarViewController = TabBarViewController()
:
fileprivate lazy var tabBarViewController: TabBarViewController = {
let storyboard = UIStoryboard(name: "TabBarStoryboard", bundle: nil)
return storyboard.instantiateViewController(withIdentifier: "MyTabBarController") as! TabBarViewController
}()
在您的TabBarViewController中,将其添加到 viewDidLoad
设置标签栏的高度:
if let tabBar = self.tabBar as? MyTabBar {
tabBar.tabBarHeight = self.tabBarHeight
}
现在,如果一切正确,那么应该有一个所需大小的标签栏,并且动画应该正确运行,因为标签栏的高度不再由viewDidLayoutSubviews方法控制。
Now if you get all that correct you should have a tab bar the size you want and the animation should work correctly because the height of tab bar is not longer controlled by the viewDidLayoutSubviews method.
我不得不使用情节提要来保存基本的UITabBarController,因为否则我无法找到一种方法来设置其UITabBar属性的类(如果有人知道添加注释的方法)。
I had to use a storyboard to hold the basic UITabBarController because I couldn't find a way to set the class of its UITabBar property otherwise (if anyone knows a way add a comment.
如果难以遵循,我已将您的项目版本上传到Dropbox,这是链接: PlayWiz-NewVersion.zip 。请小心,因为它会解压缩到相同的目录结构,因此请将其解压缩到
In case this is difficult to follow I have uploaded my version of your project to dropbox and this is the link: PlayWiz-NewVersion.zip. Be careful as it will unzip to the same directory structure so extract it to a different folder than the original otherwise you will lose the original.
该方法似乎可以正常工作对我来说是正确的,我认为没有任何问题,只能先进行全面测试。
That method appears to work correctly for me and I see no reason for there to be any problem but test it thoroughly first.
这篇关于hidesBottomBarWhenPushed使UITabBar变为“跳转”状态。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!