5
votes

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.

5

5 Answers

6
votes

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.

5
votes

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).

2
votes

For example if secondViewController is the second viewController for your second tabbar, you should do like this:

[secondViewController.view addSubview:theViewYouWantToShow];

1
votes

In iOS , Modal View Controller is always have highest priority in all view controllers available. So you can not use Modal View Controller in your case.

If you just want to show popup on the screen with back ground visibility, then just use UIAlertView. You can add OK or CANCEL button as per your requirement to remove alert view.

Or

If you want to show a full view with tab bar visibility, then add the view in a that tab as a subview. You can give it a feel like a pop up using transform property of a view.

1
votes

You can present it modally by setting PresentationStyle.This style presents viewController in a square and it does not occupy full screen.

self.modalPresentationStyle = UIModalPresentationFormSheet;

You can also set the transition:

self.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;