Timering

Purp

Custom Segue, Unwind Segue Swift 3 - UIAnimation and NavigationController

In this tutorial I show the newest IOS10 Swift 3 version of my old Objective-C tutorial. We will create a Swift 3 Xcode 8 Single View Project with subclassed Custom Segue and Custom Unwind Segue. The customization will be a UIAnimation using NavigationController.

Steps

  1. Basic settings - Storyboard, Segue, Custom Class
  2. Send Data with show Segue - test it
  3. Create UnwindSegue
  4. Subclass Custom Segue - overwrite perform function with UIAnimation
  5. Subclass Custom UnwindSegue - overwrite perform function too

Video

1. Basic settings - Storyboard, Segue, Buttons

First Create new Swift Project, and add a second ViewController to your Storyboard. Subclass it (add new File named:SecondViewController, type: Cocoa Touch Class, language:swift, subclass:UIViewController), and set at storyboard the class of the ViewController "SecondViewController". 

Don't forget, as a Swift project, you need to select at the Module Field your own Project name! 

Custom Class Swift 3 Slelect Module project name

Add an UIButton to the first ViewController named "CustomSegue", and add another one to the SecondViewController, and named "CustomUnwindSegue". 

Create a "Show" Segue from the "CustomSegue" button to the SecondViewController (ctrl+drag). Named:"CustomSegue". 

Now your Project looks like this:

01 custom segue storyboard settings

Now select the first ViewController, and embed NavigationController (Menu: Edit -->Embed in -->NavigationController).

2. Send Data with segue

In SecondViewCotnroller.swift create "colorstring" NSString object. 

In ViewController.swift file create the prepareSegue method:

In SecondViewController.swift file set the backgroundColor purple, if the "colorstring" is "purple":

Now you can test it in simulator. If you see purple SecondViewController, everything's fine. 

Test segue send data in Swift 3

3. Create Unwind Segue

In ViewCotnroller.swift write an unwind IBAction, and set the backgroundColor for cyanColor:

In the Storyboard drag the SecondViewController's Button with ctrl to the Exit, and select the create unwind function. 

unwind segue exit storyboard

If you click the CustomUnwindSegue button, and see the first ViewController with cyan backgroundColor, you can go forward for the customization. 

4. Subclass Custom Segue

3.1 Set custom kind of Segue, subclassed it and embed NavigationController in the first ViewController.

At the Inspector at the CustomSegue, select at Kind: "Custom".  Now you have a warning, that says, you have to create a custom class, so do it. (Right click - > New File --> Cocoa Touch Class -->Language:Swift, Named:CustomSegue, subclass:UIStoryboardSegue). 

custom sgeue have to custom class warning

add custom segue subclass swift file

And in the Inspector at the Class Field enter this new Class name.

set custom class to the segue

If you have an error later, you should select your Project Name, so your Target at the Module Field. Maybe it will not appear, in that case you should open your storyboard as a Source Code and add it to your <connection> code as a customModule after your customClass. Example: 

For using Custom Segue, you must to override perform function. There we can detect the Source View and the destination View. After that you can create an easy UIAnimation with the Views' positions. 

Now you can test it, and see, that your SecondViewController comes from top instead of side.

5. Subclass Custom UnwindSegue

Create a New Cocoa Touch File named: UnwindCustomSegue (subclass:UIStoryboardSegue, language:Swift). At Storyboard, add Custom Class name to your Unwind Segue:

Custom_unwind_segue_class_named_swift3

And let the same source and destination Views and window hierarchy in CustomUnwindSegue class perform function. You can use the same UIAnimation too, just with another parameters:

6. Test it at last time! 

Check the CustomUnwindSegue animation toot, it should disappear to top, and first ViewController should appear from bottom. 

NEXT

Check the IOS10 User Notification framework tutorial in Swift 3 and Objective C too

Share & Follow

If you would like to be notified of my new solutions, please subscribe to my Newsletter or my youtube channel and/ or follow me on Facebook or Twitter! Check Footer section for these opportunities!