This tutorial will show you how to use a great piece of code that was originally written in Objective-C by ttippin84 and released on Github. I converted the code to swift and forked it over here to add the swift code.

I created a very simple example application that simulates the problem.

Screen Shot 2014-09-27 at 3.31.27 PM

When you begin editing a UITextField the code will move the control so that it is within 20 pixels of the keyboard.

Screen Shot 2014-09-27 at 3.35.03 PM

Start off by adding the ResponsiveTextFieldViewController.swift file into your project.

The code for the ViewController is very simple. All you have to do is inherit from ResponsiveTextFieldViewController instead of UIViewController and the ResponsiveTextFieldViewController class will handle everything for you. It will automatically wire up the begin and end edit actions to the code in the ResponsiveTextFieldViewController base class.

That is all there is to it.  The below code is is for your cutting and pasting pleasure or for online perusal.  You can download the full project for my sample application here: KeyboardTest2



I recently did an example with a UITableView control and I noticed it did not refresh until you force the TableView to scroll.  Then all of a sudden the new items I added to my array would show up.  Some googling led me to this article which explains to use performSelectorOnMainThread in Objective-C like this:

In another article someone showed that you can use some C++ functions to perform this in swift a little nicer like this:

In C# and WPF we use a similar concept called a Dispatcher.  I simplified this a little but and wrote a little Dispatcher function for Swift that makes dispatching code to the main UI thread very clean and simple. Here is the Dispatcher code:

So if you want to solve the problem I had with the UITableView not updating you would simply write this code:

You can download the code here:  Dispatcher.swift

This article describes how to add a MapKit map to your iOS application.  It shows the basics and even centers the map on a Latitude and Longitude using Swift.

The basic steps in the video are:

  1. Create an iOS application using the Swift language.
  2. Add the MapKit framework library to the project.
  3. Add the map to the view.
  4. Make a reference to the MapKip map control in your view model.
  5. Write a little code to center the map.

So I have been developing for iOS using cheats for a really long time and now that Swift is in beta I am going to try and convert over to using that instead of Xamarin and Appcelerator. I will be creating a series of blog posts outlining the things I have learned. Most of what I am discussing will be related to a Microsoft Developer converting to an Apple world.

The first thing you need to do is click Show the assistant editor which is the icon in xcode that looks like a tuxedo.  This displays the view controller code next to the storyboard designer.Main_storyboard_and_1__bash



Next, you will right click the UI control (in this case, the label that reads “First View”) and drag into the code on the right and drop inside the class definition above the viewDidLoad method.  You will see a blue line while you are dragging.  If you do not see that and your control moves you did not right click.



When you release the right button xcode will display a small callout editor window.  This allows you to name the variable that will reference the UI control.  I named mine lblTitle in this example.

Now you will see a variable in your code.  You can use this variable to reference the control.Main_storyboard_—_Edited

To demonstrate using the variable I set the text property of the label control I referenced to the text “Title”.  As you can see, I put this code in the viewDidLoad method.


If you run the code in the simulator you will see the text for that label does in fact change when the view is displayed.

This is a very basic example of how to reference a UI component and set a property with swift.