1
2
3
4
5
6

Using Custom Unwind Segues with Xamarin.iOS

As you probably know by now, I'm a huge advocate for Xamarin technologies for native iOS and Android apps. After trying to implement some custom unwind segues for an iPad app, I discovered that no-one had documented how to do them using Xamarin.iOS yet, so I wanted to throw something together to hopefully save others time down the road.

I've created a Github repo at the link below which includes two sample projects. One sample project shows custom segues and custom unwind segues within a UINavigationController, and the other sample shows how to do custom segues without a navigation controller. The second sample is largely ported from this article.

Xamarin Custom Segue Samples - Github

I also recorded a 15 minute screencast showing how to do it all, you can find that here:
Xamarin Custom Unwind Segue Screencast - YouTube

Also, folks are frequently curious what C# code written in Xamarin.iOS looks like as compared to Objective-C, so below are two custom segues - the first in Obj-C (from the source link above), and the ported C# that does the same thing:

Objective-C:

@implementation CustomUnwindSegue
 
- (void)perform {
    UIViewController *sourceViewController = self.sourceViewController;
    UIViewController *destinationViewController = self.destinationViewController;
 
    // Add view to super view temporarily
    [sourceViewController.view.superview insertSubview:destinationViewController.view atIndex:0];
 
    [UIView animateWithDuration:0.5
            delay:0.0
            options:UIViewAnimationOptionCurveEaseInOut
            animations:^{
                // Shrink!
                sourceViewController.view.transform = CGAffineTransformMakeScale(0.05, 0.05);
                sourceViewController.view.center = self.targetPoint;
            }
            completion:^(BOOL finished){
                [destinationViewController.view removeFromSuperview]; // remove from temp super view
                [sourceViewController dismissViewControllerAnimated:NO completion:NULL]; // dismiss VC
            }];
}
 
@end

C#:

public override void Perform()
{
    // Add view to super view temporarily
    SourceViewController.View.Superview.InsertSubview(DestinationViewController.View, 0);

    UIView.Animate(0.5f, 0.0f, UIViewAnimationOptions.CurveEaseInOut, 
        () => {
            SourceViewController.View.Transform = CGAffineTransform.MakeScale(0.05f, 0.05f);
            SourceViewController.View.Center = this.TargetPoint;
        },
        () => {
            DestinationViewController.View.RemoveFromSuperview();
            SourceViewController.DismissViewController(false, null);                    
        });
}

Simple UI Example Numero Uno

A few weeks ago I mocked up a UI and tried to create it. I got it pretty far but put it aside after one night and thought I had lost my work. Tonight I found it and was pretty surprised to see how much I already wanted to refactor after just a couple of weeks of continued reading.

I am publishing the UI as a click-once app so it can be run online, and I am posting the source too for anyone that might want to hax0r at it. The app shows off a simple reflection as well as a series of custom textboxes which grow when they gain focus. There are tons of things I would like to improve in this UI mockup, but I would rather get it posted now and move on to something more challenging.

Click the screenshot below to play with the app online

Update: Source code now attached.