ios 隐藏标签栏并删除空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37040313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hiding the tabbar and removing the space
提问by Utku Dalmaz
Is there a way to hide tabbar and remove that space left (around 50px) ?
有没有办法隐藏标签栏并删除剩下的空间(大约 50 像素)?
I tried
我试过
self.tabBarController?.tabBar.hidden = true
self.extendedLayoutIncludesOpaqueBars = true
No luck. I see blank space.
没运气。我看到空白。
回答by Smnd
回答by Alessandro Ornano
Swift 3:
斯威夫特 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
}
}
To use (if for example self
is a UITabBarController
):
使用(如果例如self
是 a UITabBarController
):
self.setTabBarVisible(visible: false, duration: 0.3, animated: true)
Swift 2.x:
斯威夫特 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
}
}
To use:
使用:
self.tabBarController?.setTabBarVisible(visible: false, duration: 0.3, animated: true)
回答by JZAU
After saw your screenshot in comment. I think you can try to set hidesBottomBarWhenPushed
to true.
在评论中看到你的截图后。我想你可以尝试设置hidesBottomBarWhenPushed
为true。
hidesBottomBarWhenPushed = true
Or storyboard.
或者故事板。
It will hide bottom bar automatically when you pushed to another view controller, and appear it again when you go back.
当你推到另一个视图控制器时它会自动隐藏底部栏,并在你返回时再次显示它。
回答by Itumeleng Mabote
Programmatically, add this to the next view controller for swift 4.
以编程方式,将它添加到 swift 4 的下一个视图控制器。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
tabBarController?.tabBar.isHidden = true
edgesForExtendedLayout = UIRectEdge.bottom
extendedLayoutIncludesOpaqueBars = true
}
And add a background color
并添加背景色
回答by Ajay Kumar
NOTE - This solution is to just to remove white space left after hiding tab bar.
注意 - 此解决方案只是删除隐藏标签栏后留下的空白。
For hiding tab bar best solution is - @Michael Campsall answer here
隐藏标签栏的最佳解决方案是 - @Michael Campsall在这里回答
The simplest solution to this is to change your view's(in my case its tableView) bottom constraints, instead of giving bottom constraints with BottomLayoutGuide give it with superview. Screenshots attached for reference.
对此最简单的解决方案是更改您的视图(在我的情况下是它的 tableView)底部约束,而不是使用 BottomLayoutGuide 给底部约束给它超级视图。附上截图供参考。
Constraints shown in below screenshots creates the problem, change it according to next screenshot.
以下屏幕截图中显示的约束会产生问题,请根据下一个屏幕截图进行更改。
Actual constraints to remove white space should be according to this(below) screenshot.
删除空白的实际约束应该根据这个(下面)截图。
回答by budidino
For those that like to do everything programmatically, add this line to the init
method of a ViewController
that shouldn't have the tabBar:
对于那些喜欢编程做的一切,这行添加到init
的方法ViewController
是不应该使用TabBar:
hidesBottomBarWhenPushed = true
回答by valencieu
The third answer on this questionworks for me in the following way:
关于这个问题的第三个答案对我有以下作用:
The code on my view controller
我的视图控制器上的代码
@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
}
In storyboard:
在故事板中:
The view controller main view background color is black color:
Then you could have another view inside (background color white), constrained trailing and leading space to superview and top and bottom space to the layout guide.
然后你可以在里面有另一个视图(背景颜色为白色),约束到超级视图的尾随和前导空间以及布局指南的顶部和底部空间。
And the result is:
结果是:
回答by Sulthan
My preferred way to do that is using a wrapping controller. If I want to hide the tab bar, I just increase the height of the tab bar controller, thus effectively the tab bar is moved out of the screen.
我的首选方法是使用包装控制器。如果我想隐藏标签栏,我只需增加标签栏控制器的高度,从而有效地将标签栏移出屏幕。
With this solution you don't need to hack tab bar frame and you don't depend on navigation controller push 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
}
}
}
回答by Balaji Ramakrishnan
Yes. You can hide your tab bar when you push to view controller. You can show tab bar in your home. You can hide your tab bar when you push to next View controller.
是的。当您推送到视图控制器时,您可以隐藏您的标签栏。您可以在家中显示标签栏。当您推送到下一个视图控制器时,您可以隐藏标签栏。
See the Hide Botton Bar on Pushfollowing image and set in all viewcontrollers where you dont want tab bar.
请参阅下图推送时隐藏底部栏,并在所有不需要标签栏的视图控制器中进行设置。
Hope it helps..
希望能帮助到你..
回答by Dhaval H. Nena
I was facing the same issue and root cause was BOTTOM CONSTRAINT
我遇到了同样的问题,根本原因是BOTTOM CONSTRAINT
Make sure you set the bottom constraint of your bottom most viewin the main view hierarchy with SUPERVIEW, NOT "SAFE AREA"
确保使用SUPERVIEW 而不是“SAFE AREA”在主视图层次结构中设置最底部视图的底部约束
Hope this helps someone..
希望这可以帮助某人..