What is the correct way to view the overlay of all the other viewControllers in the application?

I am relatively new to iOS development and planning an application. The application will have several different scenes / ViewControllers that occupy the entire screen.

I intend to always have a small button in the upper left corner of the screen that will return the user to the home page, depending on which of the vc is currently showing.

I could place a separate but identical button in each viewController view, but that seems inelegant and fragile.

My instinct after reading the View Controller Programming Guide is to create a View Controller container that will be the root vc - this vc will have two child vcs - one with a home button, and the other under the fact that all the other VCs (effectively turning into a new vc root. )

Does this sound like a smart solution for more experienced iOS developers? Is there a simpler solution?

Any advice, fortunately, received.

+4
source share
2 answers

I think that is probably your best decision. In iOS, the ViewController takes up all the free space for viewing. Without a container view that you control yourself, any view controller that you install will occupy the entire window, covering everything that you displayed.

However, there is an alternative approach that could be simpler, depending on your project.

You can create a UIView object (your button) in some central place (say, your application delegate class). There may be a button in this view that you attach to the method:

@implementation AppDelegate - (void) someSetupMethod { UIButton* b = [UIButton buttonWithType:UIButtonTypeCustom]; // setup whatever properties // Now set button to call a method *on this AppDelegate object* ("self"): [b addTarget:self action:@selector(homeButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; // Store the button definition somewhere permanant self.reusableHomeButton = b; } - (void) homeButtonTapped:(id)sender { // do something to pop view controllers back to what you want. } 

Then, in your view controllers, they can show the home button when they appear:

 - (void) viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate; [self.view addSubview:appDelegate.reusableHomeButton]; } 

This uses the intentional side effect of [view addSubview:...] , which is that if the view is already in some parent, it first removes it from that parent and then adds it to the new view.

It also exploits the fact that a button can send a message to any object. It should not be a ViewController that will close the parent view of the button.

This causes your button to "move" from one displayed .view view controller to a new one when a new controller is introduced.

Since the button has the purpose of the AppDelegate object (and therefore sends this message to this object), it works "from anywhere" if there is an application delegation object to receive the message (which it has been doing for as long as your application has been running).

+1
source

With Swift, you can use the capabilities of protocols and protocol extensions. I wrote an article about this approach. In short, you will need the following:

  • Create a protocol that describes the overlay view controller.
  • Encapsulate the presentation / rejection logic of the presentation controller in this protocol using a protocol extension.
  • Create a protocol for the overlay host.
  • Encapsulate the logic for instantiating the overlay view controller from the storyboard to this protocol using the protocol extension.

And you are all set. Usage will look like this:

 class ViewController: UIViewController, OverlayHost { @IBAction func showOverlayButtonPressed() { showOverlay(type: YourOverlayViewController.self, fromStoryboardWithName: "Main") } } 

Source code: https://github.com/agordeev/OverlayViewController

0
source

All Articles