Using Custom Unwind Segues with Xamarin.iOS
- #Storyboards
- #UI Sample
- #Xamarin
- #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);
});
}