1

I would like to check whether a certain UIViewController was modally presented or not. In addition to that I would like to differ if it is modally presented in the TabBar or not. I found the following extension under this link:

extension UIViewController {
    var isModal: Bool {
        if let index = navigationController?.viewControllers.index(of: self), index > 0 {
            return false
        } else if presentingViewController != nil {
            return true
        } else if navigationController?.presentingViewController?.presentedViewController == navigationController  {
            return true
        } else if tabBarController?.presentingViewController is UITabBarController {
            return true
        } else {
            return false
        }
    }
}

However, this extension does not differ between the UIViewController being presented in the TabBar or out side the tab bar (I get the same result). Removing the last else if still returns the same Boolean (both return true) and so does removing the second last one (both return false).

I would like to add a "go back" button if it's been presented outside the tabbar.

EDIT:

I have a tabbar under which the user can open his own profile. If that tabbaritem is tapped, the user gets to see his own profile (presented through tabbar). However, he may also get to see his own profile by tapping his own username in another tabbaritem (e.g. in the home feed). In the latter case (since there has been another view controller before his own profile) I want the user to be able to go back to the home feed again (which wouldn't be possible in the former).

Moritz
  • 745
  • 1
  • 10
  • 32
  • I faced this problem and I checked if self.navigationController?.popViewController(animated: true) == nil so it is presented modal so try this may work for you – Hosny Jun 16 '17 at 16:46
  • It passes the if in both cases, so this doesn't seem to work either. – Moritz Jun 16 '17 at 16:51
  • Could you add a bit more context? What's the problem you're actually trying to solve here? – pob21 Jun 16 '17 at 18:48
  • @pob21 Edited my question – Moritz Jun 16 '17 at 18:54
  • Still a bit confused (correct me if I'm wrong) but it sounds like one of your tabbaritems is the "User Profile", but the content in this tab can also be accessed from the the home feed (another tab) correct? – pob21 Jun 16 '17 at 18:59
  • Meaning you're pushing the "User Profile" view controller onto the navigation stack from the home screen? – pob21 Jun 16 '17 at 18:59
  • 1. Thats' right, if the user cannot go back to a view Controller I don't want the ability to go back (button to go back simply because there is none). I used the own profile as an example but any other profile – Moritz Jun 16 '17 at 19:00
  • That seems like it solves itself. If the root viewcontroller of your "Home" tab is a `UINavigationController` that means that pushing the "My Profile" view controller onto the navigation stack from there will automatically give you a "back" button. What am I missing here? – pob21 Jun 16 '17 at 19:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/146902/discussion-between-moritz-and-pob21). – Moritz Jun 16 '17 at 19:03

0 Answers0