Scroll to the Bottom of a Collection View

private func scrollToBottom() {
    let lastSectionIndex = (collectionView?.numberOfSections())! - 1
    let lastItemIndex = (collectionView?.numberOfItemsInSection(lastSectionIndex))! - 1
    let indexPath = NSIndexPath(forItem: lastItemIndex, inSection: lastSectionIndex)
    collectionView!.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Bottom, animated: false)


Add a UITableView to an Existing ViewController in Xcode

Quick and dirty checklist for adding a table view to an existing view controller:

  1. Embed existing view controller in a NavigationController
  2. Drag a UITableView object into the view controller in the storyboard
  3. Make the UITableView the same width and height as the view controller and set the constraints to all four sides with no margins
  4. Create an outlet and connection for the tableView in the view controller class
  5. Add UITableViewDataSource and UITableViewDelegate to the class declaration. Your class should look something like this
    import UIKit
    class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
        @IBOutlet weak var tableView: UITableView!
        override func viewDidLoad() {
  6. Select the table view, open the connections inspector and connect the table view’s dataSource and delegate methods to the View Controller object.
  7. If you’re not using static cells, open the attributes inspector and set the number of Prototype Cells to 1

Create a Confirmation Alert in iOS with Swift

If you want to present an alert with an option to either take an action or cancel:

@IBAction func clearImage(sender: UIBarButtonItem) {
    let alert = UIAlertController(title: "Clear Canvas", message: "Are you sure you want to clear the canvas?", preferredStyle: .Alert)
    let clearAction = UIAlertAction(title: "Clear", style: .Destructive) { (alert: UIAlertAction!) -> Void in
        self.canvas.image = nil
    let cancelAction = UIAlertAction(title: "Cancel", style: .Default) { (alert: UIAlertAction!) -> Void in
        //print("You pressed Cancel")
    presentViewController(alert, animated: true, completion:nil)


Create Navigation Bar Programmatically in Swift

In your UIPopoverPresentationControllerDelegate:

func presentationController(controller: UIPresentationController, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? {
    let presentedViewController = controller.presentedViewController
    let navigationController = UINavigationController(rootViewController: presentedViewController)
    let dismissButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: "dismissPopover:")
    presentedViewController.navigationItem.rightBarButtonItem = dismissButton
    return navigationController
func dismissPopover(sender: AnyObject) {
    self.dismissViewControllerAnimated(true, completion: nil)

Setting Up Delegates in iOS

This is copied from the iOS Apprentice tutorial on but I wanted to put it where I could easily access it. These are the basic steps for setting up a delegate pattern between two objects, where object A is the delegate for object B, and object B will send messages back to A.

  1. Define a delegate protocol for object B
  2. Give object B an optional delegate variable. This variable should be weak
  3. Make object B send messages to its delegate when something interesting happens, such as the user pressing the Cancel or Done buttons, or when it needs a piece of information. You write delegate?.methodName(self, ...)
  4. Make object A conform to the delegate protocol. It should put the name of the protocol in its class line and implement the methods from the protocol.
  5. Tell object B the object A is now its delegate


Override Application Transport Security in xCode

When you get the error App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app’s Info.plist file., you can override the security and allow all http urls through by adding this to your info.plist file:


However, before doing this, check to see if there is an https version of the url. That will work just as well, and https won’t throw that error.

Create keyboard shortcut to toggle Document Outline in Xcode

In Xcode, open Preferences and go to the “Key Bindings” section. Type “outline” in the search filter box. Select “Show Document Outline” and double-click to the right, in the “Key” column. A text box should appear and you can type in your new shortcut. I used Cmd+9.