隐藏标签栏并删除空间
有没有办法隐藏tabbar和删除剩下的空间(约50px)?
我试过了
self.tabBarController?.tabBar.hidden = true self.extendedLayoutIncludesOpaqueBars = true
没有运气。 我看到空白。
如果您仍然在隐藏的标签栏下看到黑色条纹,您是否尝试过在不透明条纹下select“ 延伸边缘” ?
确保仍然select“底部酒吧” 。 希望能帮助到你!
Swift 3 :
extension UITabBarController { func setTabBarVisible(visible:Bool, duration: TimeInterval, animated:Bool) { if (tabBarIsVisible() == visible) { return } let frame = self.tabBar.frame let height = frame.size.height let offsetY = (visible ? -height : height) // animation UIViewPropertyAnimator(duration: duration, curve: .linear) { self.tabBar.frame.offsetBy(dx:0, dy:offsetY) self.view.frame = CGRect(x:0,y:0,width: self.view.frame.width, height: self.view.frame.height + offsetY) self.view.setNeedsDisplay() self.view.layoutIfNeeded() }.startAnimation() } func tabBarIsVisible() ->Bool { return self.tabBar.frame.origin.y < UIScreen.main.bounds.height } }
使用(如果self
是一个UITabBarController
):
self.setTabBarVisible(visible: false, duration: 0.3, animated: true)
Swift 2.x:
extension UITabBarController { func setTabBarVisible(visible:Bool, duration: NSTimeInterval, animated:Bool) { if (tabBarIsVisible() == visible) { return } let frame = self.tabBar.frame let height = frame.size.height let offsetY = (visible ? -height : height) // animation UIView.animateWithDuration(animated ? duration : 0.0) { self.tabBar.frame = CGRectOffset(frame, 0, offsetY) self.view.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height + offsetY) self.view.setNeedsDisplay() self.view.layoutIfNeeded() } } func tabBarIsVisible() ->Bool { return self.tabBar.frame.origin.y < UIScreen.mainScreen().bounds.height } }
使用:
self.tabBarController?.setTabBarVisible(visible: false, duration: 0.3, animated: true)
在看到你的截图评论。 我想你可以尝试设置hidesBottomBarWhenPushed
为true。
hidesBottomBarWhenPushed = true
或故事板。
当您推到另一个视图控制器时,它会自动隐藏底部条,并在您返回时再次出现。
关于这个问题的第三个答案对我有以下作用:
我的视图控制器上的代码
@IBAction func buttonPressed(sender: AnyObject) { setTabBarVisible(!tabBarIsVisible(), animated: true) } func setTabBarVisible(visible: Bool, animated: Bool) { // hide tab bar let frame = self.tabBarController?.tabBar.frame let height = frame?.size.height var offsetY = (visible ? -height! : height) print ("offsetY = \(offsetY)") // zero duration means no animation let duration:NSTimeInterval = (animated ? 0.3 : 0.0) // animate tabBar if frame != nil { UIView.animateWithDuration(duration) { self.tabBarController?.tabBar.frame = CGRectOffset(frame!, 0, offsetY!) self.view.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height + offsetY!) self.view.setNeedsDisplay() self.view.layoutIfNeeded() return } } } func tabBarIsVisible() -> Bool { return self.tabBarController?.tabBar.frame.origin.y < UIScreen.mainScreen().bounds.height }
在故事板中:
视图控制器主视图的背景色是黑色的:
然后你可以在里面看到另一个视图(背景颜色为白色),限制拖尾和引导空间到超视图和顶部和底部空间到布局指南。
结果是:
是。 当您按下查看控制器时,您可以隐藏标签栏。 你可以在你的家中显示标签栏。 当您推送到下一个视图控制器时,您可以隐藏标签栏。
请参阅隐藏推送酒吧推下面的图像,并在所有您不希望标签栏的视图控制器中设置。
希望能帮助到你..
我的首选方法是使用包装控制器。 如果我想隐藏标签栏,我只是增加标签栏控制器的高度,从而有效地将标签栏移出屏幕。
有了这个解决scheme,你不需要破解标签栏框架,你不依赖导航控制器推动animation:
import UIKit class ViewController: UIViewController { let tabController: UITabBarController = { let tabController = UITabBarController() // setup your tabbar controller here return tabController; }() var tabbarHidden = false { didSet { var frame = self.view.bounds; if (tabbarHidden) { frame.size.height += self.tabController.tabBar.bounds.size.height; } self.tabController.view.frame = frame; } } override func viewDidLoad() { super.viewDidLoad() // add the tab controller as child controller addChildViewController(self.tabController) self.tabController.view.frame = self.view.bounds self.tabController.view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight] self.view.addSubview(self.tabController.view) self.tabController.didMoveToParentViewController(self) // for debugging let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(switchTabbar)) self.tabController.view.addGestureRecognizer(tapRecognizer) } override func childViewControllerForStatusBarStyle() -> UIViewController? { return self.tabController } override func childViewControllerForStatusBarHidden() -> UIViewController? { return self.tabController } func switchTabbar() { UIView.animateWithDuration(0.3) { self.tabbarHidden = !self.tabbarHidden } } }
注 – 这个解决scheme只是为了在隐藏标签栏后删除剩余的空白。
对于隐藏标签栏的最佳解决scheme是 – @Michael Campsall 在这里回答
最简单的解决scheme是改变你的视图(在我的情况下,它的tableView)底部的约束,而不是给BottomLayoutGuide使用超视图的底部约束。 附上截图供参考。
以下屏幕截图中显示的约束会产生问题,请根据下一屏幕截图进行更改。
删除空白的实际限制应该根据这个(下面)截图。
对于那些喜欢以编程方式做所有事情的人来说,将这一行添加到不应该有tabBar的ViewController
的init
方法中:
hidesBottomBarWhenPushed = true
以编程方式将其添加到swift 4的下一个视图控制器。
override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) tabBarController?.tabBar.isHidden = true edgesForExtendedLayout = UIRectEdge.bottom extendedLayoutIncludesOpaqueBars = true }
并添加一个背景颜色