Return’s Key

Standard

Got an iOS app with several text fields? Something like this:

Screen Shot 2014-04-10 at 22.21.29

You have? Well, you can add a nice bit of polish by making the keyboard’s return button toggle between them. In this example, the return key should read “Next” for the first two fields, then “Done” for the last one.

Users can then fill in the entire screen just using the keyboard; without having to tap the correct text field each time. They’ll love and worship you, and buy you wonderful gifts. Probably.

You can set this up manually, but it’s a bit of a pain:

FirstNameField.ReturnKeyType = UIReturnKeyType.Next;
FirstNameField.ShouldReturn = (textField) =>
{
    MiddleNameField.BecomeFirstResponder();
    return true;
};

MiddleNameField.ReturnKeyType = UIReturnKeyType.Next;
MiddleNameField.ShouldReturn = (textField) =>
{
    LastNameField.BecomeFirstResponder();
    return true;
};

LastNameField.ReturnKeyType = UIReturnKeyType.Done;
LastNameField.ShouldReturn = (textField) =>
{
    SubmitName();
    return true;
};

Dull, Boring and Repetitive. Everything code shouldn’t be.

How about doing this instead?

this.SetupFieldOrder(new [] { FirstNameField, MiddleNameField, LastNameField }, SubmitName);

Stick TextFieldOrderExtension.cs in your project, and you can.

Call it from ViewDidLoad in a UIViewController. The method takes two arguments:

  1. An array of UITextField objects, in the order they should be filled out
  2. An Action to be called when return is tapped on the final text field (optional)

If you leave out the second argument, it’ll just loop back round to the first text field. It even sorts out the return key to read the appropriate action: “next” or “done”.

There’s also an example project on Github.

Easier iOS Launch Images

Standard

iOS launch images can drive you a bit mad. For a universal iPhone and iPad app targeting both iOS 6 and 7 you now need 13 different images. It’s tedious. It’s no wonder so many people cheat and use a logo, but that’s against Apple’s Human Interface Guidelines. Oh, and it looks pants.

iOS 7 had the added complication of the status bar being transparent, so you have to include that area in the launch image. Compare the launch image and a screenshot from ScriptTimer

I was going through the laborious process of hiding all labels, taking a screen shot, bunging it in an image editor and removing the status bar. But I’ve got better things to do with my time.

Luckily it’s pretty easy to programatically hide the status bar and any text in the view hierarchy…

  1. Add LaunchImage.cs to your iOS project
  2. Add one line to your AppDelegate:
    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
    	...
    
    	#if LAUNCHIMAGE
    		LaunchImage.SayCheese();
    	#endif
    
    	return true;
    }
  3. Add LAUNCHIMAGE as a compilation symbol (Property Options > Compiler in Xamarin Studio):

  4. Launch the app, take a screenshot, and you’ve got a ready-to-go launch image. Tada!

I’ve used a compilation symbol to avoid any chance of App Store rejection, as we’re dicking around with the internals of iOS’s status bar, which will make Apple cry.