In my tab based app, on clicking one of the tabs, I want to display a modal view controller with some info. In my app delegate's didSelectViewController method, I am adding modal view. But it occupies entire screen and hides the tab bar. I don't want to hide the tab-bar, just want to display modal-view which pops up and can be dismissed.
How do I do it?
Please help.
Thanks in advance.
If you don't want that behavior, you should set hidesBottomBarWhenPushed to true where applicable. This will hide the tab bar along with any toolbars you had showing, but only when a view controller is pushed onto the navigation stack. This allows you to show the tab bar at first, then hide it when you need more room.
Simply, Go to ViewController (in StoryBoard) -> Attribute inspector -> Under 'View Controller' section select 'Hide Bottom Bar on Push' checkbox. This works like a charm.
In case you're using tabbar with navigation controller hidesBottomBarWhenPushed will not work, but tabBarController. tabBar. hidden will do.
Answer: Use self. tabBarController?. tabBar. hidden instead of hidesBottomBarWhenPushed in each view controller to manage whether the view controller should show a tab bar or not.
Each content view controller is designated as the view controller for one of the tabs in the tab bar view. When a tab is tapped by the user, the tab bar controller object selects the tab and displays the view associated with the corresponding content view controller.
When a tab bar view is part of a tab bar interface, it must not be modified. In a tab bar interface, the tab bar view is considered to be part of a private view hierarchy that is owned by the tab bar controller object.
The tab bar controller has its own container view, which encompasses all of the other views, including the tab bar view. The custom content is provided by the view controller of the selected tab. When a tab bar view is part of a tab bar interface, it must not be modified.
When a tab bar view is part of a tab bar interface, it must not be modified. In a tab bar interface, the tab bar view is considered to be part of a private view hierarchy that is owned by the tab bar controller object. If you do need to change the list of active tabs, you must always do so using the methods of the tab bar controller itself.
Modal view controllers are always presented full screen on an iPhone. If you don't want to hide the tab bar, then you need to present this view in some other way besides modal.
I was able to "present" a new view controller, over another, UNDER the tab bar, by setting modalPresentationStyle
to .currentContext
.
let newViewController = UIViewController()
newViewController.view.backgroundColor = UIColor.red
newViewController.modalPresentationStyle = .currentContext
newViewController.modalTransitionStyle = UIModalTransitionStyle.flipHorizontal
present(newViewController, animated: true, completion: nil)
Edit: From more testing, the above can have some buggy behavior if someone changes the tab WHILE the newViewController
is presented.
To "fix," I created a "switcher" - a UIViewController
that animates between the view controllers that I want to flip UNDER the tab bar:
view.addSubview(nextView)
UIView.transition(from: currentView,
to: nextView,
duration: 0.5,
options: animation,
completion: { (_) in
currentView.removeFromSuperview()
})
In this case, currentView
is the view of ViewControllerOne (the currently visible one), and the nextView
is view of ViewControllerTwo (the one we want to present).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With