šŸ“œ ā¬†ļø ā¬‡ļø

Native segue from left to right in iOS

I warn you right away, this is a trick. It will suit not everyone and not always, but if you need to display a window with some information to the left of the main one, then my method will be just right.

It is quite possible that he is already known to everyone, and I invented the bicycle, but I invented it myself, after a couple of days of fruitless attempts to make the native segue from left to right, so I am happy to share.

First, some input. Segue is a way to change screens in iOS. One of the most popular varieties is push (from the version of iOS 8 - show). Push segue always replaces the current view from right to left. That is, you have, as it were, a second window on the right, and when you press a button, it moves to the left, replacing the first.
')
This behavior you see in the phone book when choosing a subscriber. At the same time, the button to return to the previous window appears at the top, and when you click on it, the desired animation occurs from left to right.

The problem is that I had to do the same thing, but in mirror image there are no standard methods for this (according to Google). There are lots of instructions on how to make custom segue with similar animation, but they all hurt the eye with its unnatural compared to the native push.

Here's what I got in the end:



Only standard methods and 0% custom animation are used. Ready example on GitHub .

Idea


The main trick is that we do not do any new segue, we use native push, for the intended purpose.

I think the meaning of this screenshot is already clear. The starting window is Info, which we need to display on the left. When it is activated, we quickly and quietly, without animation, display the Main window using push segue. And after that you can go back, using unwindFromViewController. Only the user does not know about it and sees a smooth and beautiful left to right segue.

Implementation


I will not describe how to connect with each other elements and assign custom classes, I am sure that you all know that already. In any case, you can download the source from GitHub and see.

We will need 2 icons, one custom segue and one InfoViewController.

On the Info window, we add one button with a house icon, on the right. Then we connect it to the Main window with the usual push (show) segue.

Create a custom class InfoViewController, a successor to UIViewController and assign it to the Info window:
#import "InfoViewController.h" @interface InfoViewController () @end @implementation InfoViewController //     Main    //  custom segue   FastSegue - (void) viewDidLoad { [self performSegueWithIdentifier:@"FastSegue" sender:nil]; } //       Info - (IBAction)unwindFromViewController:(UIStoryboardSegue *)sender { } @end 


Now we need to create a custom segue that will quietly display the Main window when the application starts:
 #import "FastSegue.h" @implementation FastSegue //  push segue,    - (void) perform { [[[self sourceViewController] navigationController] pushViewController:[self destinationViewController] animated:NO]; } @end 

And now you need to create an Info Info controller linked to Main from this. This link must be assigned the identifier FastSegue.



Things are going to be small - make a return to the Info window when you click on the button in the Main window. This is an optional part, because, by default, the return button with the arrow and the name of the previous window will be generated there anyway, but I need an icon.
In addition, as it turned out, if you replace backBarButtonItem with your element, then you cannot open the Info window with a gesture from left to right, as with a normal segue.

So, add the UINavigationItem and leftBarButtonItem elements to the Main window to replace the automatically generated native backBarButtonItem. With a picture, select the icon ā€œiā€ and our button is successfully displayed when the application starts. Only does nothing.

In order for it to replace the functionality of the backBarButtonItem, you need to assign an action unwindFromViewController to it - that is why we designated it in the Info window controller.

So link the button to the exit point and select unwindFromViewController:

That's all!
I also add that for an inconspicuous starting switch, LaunchImage, or LaunchScreen is needed, but there are no applications without them.

If it seems to you that I have written complete nonsense and a crutch here - I will be glad to see your decision and put it into practice. I, too, this method seems not quite true, and, moreover, is not at all universal , but it works as I need and looks great!

Thanks for attention.

Updated

Replaced the old video with a little more visual, with tables.

Updated 2

As it turned out, no this is not a crutch, no this is not "unusual behavior." Similarly, the native Mail application works.

Open Mail - we immediately see the letters and at the top there is a button to open all the mailboxes. When you click on it, left to right segue is most likely done in the same way. That is, preliminary invisible unwinding on the second window.

It turns out that I reevaluated what Apple itself has been using for a long time. Hello to all disagree .

Source: https://habr.com/ru/post/239093/


All Articles