Modal view controller hides the tab bar
In a tab based application, by clicking one of the tabs, I want to display a modal view controller with some information. In my delegate's didSelectViewController method, I add a modal view. But it takes up the whole screen and hides the tab bar. I don't want to hide the tab bar, I just want to display a modal view that appears and can be dismissed.
How should I do it?
Please, help.
Thanks in advance.
source to share
I was able to "present" a new view controller, above another, below 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: Due to more tests, the above may have incorrect behavior if someone changes the WHILE tab when presented newViewController
.
To "fix", I created a "switch" - a UIViewController
that animates between view controllers 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
, this is the ViewControllerOne view (currently visible) and the nextView
ViewControllerTwo view (the one we want to present).
source to share
On iOS, the Modal View controller always has the highest priority in all available controllers. Thus, you cannot use Modal View Controller in your case.
If you just want to display the popup on the screen with backsurface visibility, just use UIAlertView . You can add "OK" or "CANCEL" button according to your requirement to remove the warning.
or
If you want to show the full view with tab bar visibility, add the view in that tab as a subtitle. You can give it a popup feel using the transform property of the view.
source to share
You can present it modally by setting PresentationStyle. This style presents the viewController squared and does not fill the entire screen.
self.modalPresentationStyle = UIModalPresentationFormSheet;
You can also set the transition:
self.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
source to share