Featured: Swift Recipes

A popular collection of answers to Stack Overflow questions, featured solutions, highly rated responses and dozens of earned bounties.

profile for SwiftArchitect on Stack Exchange, a network of free, community-driven Q&A sites

Collection of Swift Recipes, Gists and Xcode Projects.

Portrait & Landscape Orientation Mix & Match

so-logo-320x320SO-41433100 (Download Swift Recipe)

Special Instructions: None


Animating Toolbars out of sight while changing iPhone orientation

so-logo-320x320SO-41198069 (Download Swift Recipe)

Special Instructions: Change the Initial View Controller in Interface Builder to experiment with the different implementation variations. Notice that the smoothest, simplest solutions involve exactly zero lines of code.

See animation here.





Moving a Webview and a Toolbar

so-logo-320x320SO-41291054 (Download Swift Recipe)

Special Instructions: See animation here.

UITableView Cell Height iOS Bug

so-logo-320x320SO-35260912 (Download Swift Recipe)

Special Instructions: This causes `UILabel` to be incorrectly clipped in an AutoLayout environment. In the attached project, you will notice that changing the `estimatedRowHeight` value, as well as the `reloadData` invocation has various effects on the label, which is still sometimes clipped.



Automatic UITextView & UITextField using AutoLayout

so-logo-320x320SO-35259289 (Download Swift Recipe)

Special Instructions: Scrolling editable text into sight is notoriously difficult. This solution demonstrates throttled refresh, which alleviates or annihilates flickering generally associated with long text.



Change alpha of the Image of a UIButton

so-logo-320x320SO-35040233 (Download Swift Recipe)

Special Instructions: Use in Interface Builder with `@IBDesignable` and `@IBInspectable` for live preview.



Custom & Standard Separators for UITableViewCell

so-logo-320x320SO-35001392 (Download Swift Recipe)

Special Instructions: Compare implementation, and notice that there are exactly 0 lines of code delta between standard separators and in-cell separator. It is all handled at the resource level. Cell separators, in addition, will only be present when a cell exists.


Resize Table View Cells on Selection

so-logo-320x320SO-34996964 (Download Swift Recipe)

Special Instructions: None.


See animation.

Activating & Deactivating UITextField using Responder

so-logo-320x320SO-34985404 (Download Swift Recipe)

Special Instructions: Try out with successive selection, or by dismissing keyboard.
Keywords: `UIResponder` `resignFirstResponder` `becomeFirstResponder`


See animation.

Reading a text field from iOS Settings

so-logo-320x320SO-34905431 (Download Swift Recipe)

Special Instructions: Run with and without the Setting being set, compare output.



Import Objective-C Pods Framework into Swift

so-logo-320x320SO-34553684 (Download Swift Recipe)

Special Instructions: run

pod install

…then open the workspace (not the project).


Android-like ScrollView in iOS Interface Builder

so-logo-320x320SO-34139812 (Download Swift Recipe)

Special Instructions: Look at the relative relationships between the `UIScrollView`, the content `UIView`, and the 3 `UILabel` & constraints which define the sizes of what needs to be displayed.

Screen Shot 2015-12-10 at 7.36.01 AM


Size Classes in Interface Builder

so-logo-320x320SO-33987900 (Download Swift Recipe)

Special Instructions: none.



Dynamic loading images in UITableView

so-logo-320x320SO-33975355 (Download Swift Recipe)

Special Instructions: Change the number of cells to load as many Wikipedia images as you want.



Adding a Toolbar to a TableView

so-logo-320x320SO-33863028 (Download Swift Recipe)

Special Instructions: none.



UITabBar with a Blur Visual Effect

so-logo-320x320SO-33819852 (Download Swift Recipe)

Special Instructions: none. Download the 1×1 clear.png.


Target-based localization

so-logo-320x320SO-33694785 (Download Swift Recipe)

Special Instructions: This example specifically addresses multiple targets with a single localization each. For the general and preferred approach, see Shared localizations across targets.



Scroll Animation in a Label

so-logo-320x320SO-33632266 (Download Swift Recipe)

Special Instructions: none.


AVAudioPlayer, multiple, simultaneous playback

so-logo-320x320SO-32768352 (Download Swift Recipe)

Special Instructions:

  • record on
  • playback on main thread >
  • playback on background thread >>



Intercept Taps on UIWebView in order to add Popover menu

so-logo-320x320SO-32670551 (Download Swift Recipe)

Special Instructions: none.


Integrate C, C++, Objective-C, Objective-C++ & Swift in the same Xcode project (all at once)

so-logo-320x320SO-32541268 (Download Swift Recipe)

Special Instructions: none.

Hello World in C
Hello World in C++
Hello World in Objective-C
Hello World in Objective-C++
Hello World in Swift


How to let the iOS or OS X pick the San Francisco Font in a CSS 

so-logo-320x320SO-32408973 (Download Swift Recipe)  (view HTML)

Special Instructions: none.


PrepareForSegue from a UIButton inside a UITableViewCell

so-logo-320x320SO-32341629 (Download Swift Recipe)

Special Instructions: none.



Record and Play Audio on iOS using AVAudioRecorder and Swift

so-logo-320x320SO-32342486 (Download Swift Recipe)

Special Instructions: none.



Communication between a UIWebview and Embedded Javascript

so-logo-320x320SO-32322397 (Download Swift Recipe)

Special Instructions: none.


Facebook & Parse Integration iOS & Swift Tutorial

so-logo-320x320SO-32302877 without Parse (Download legacy Swift Recipe)

Special Instructions: Run `pod install`, open `.xcworkspace`.


so-logo-320x320SO-32302877 + Parse (Download Swift Recipe)

Special Instructions: Run `pod install`, open `.xcworkspace`.

icon_facebook      parse-core

Navigation Bar and Web View (UINavigationController + UIWebview) Obj-C, Swift, Storyboard

so-logo-320x320SO-32277486 (Download Swift Recipe) (Legacy Obj-C)

Special Instructions: none.


Architecture of UITabBarController & embedded UIViewController using UINavigationController

so-logo-320x320SO-32258452 (Download Swift Recipe)

Special Instructions: none.


Changing the interface orientation of a UITabBarController

so-logo-320x320SO-32255151 (Download Swift Recipe)

Special Instructions: none.


Design UITableview section header in Interface Builder

so-logo-320x320SO-31693901 (Download Swift Recipe)

Special Instructions: none.

GameCenter Multiplayer using GCHelper

so-logo-320x320SO-31699439 (Download Swift Recipe)

Special Instructions: Run `pod install`, open `.xcworkspace`. Make `authenticationChanged` public.

Integrating and using a COCOAPOD Framework in Swift

so-logo-320x320SO-31642385 (Download Swift Recipe)

Special Instructions: Run `pod install`, open `.xcworkspace`.

import PubNub
var client:PubNub?
self.client = PubNub()

Get Notified when Someone Makes an In-App Purchase

so-logo-320x320SO-32021855 (Download Swift Recipe)

Special Instructions: Run `pod install`, open `.xcworkspace`.

if let mixpanel = Mixpanel.sharedInstance() {

Add a Custom Color Palette to Interface Builder

so-logo-320x320SO-31702193 (Download Swift Recipe)

Special Instructions: Use Interface Builder to change the color.


Cross-Fade Animation while changing Text

so-logo-320x320SO-3073520 (Download Swift Recipe)

Special Instructions: none.


Move Toolbar Up & Down based on Keyboard and the Predictive Accessory View

so-logo-320x320SO-25946121 (Download Swift Recipe)

Special Instructions: none.

predictive-down predictive-up

Parallax Effect with UIScrollView

so-logo-320x320SO-22201729 (Download Swift Recipe)

Special Instructions: none.




Triggering a Segue from a custom TableViewCell in iOS

so-logo-320x320SO-19778484 (Download Swift Recipe)

Special Instructions: none.


How can an item be removed from UIPasteboard

so-logo-320x320SO-19172176 (Download Swift Recipe)

Special Instructions: none.



UITableViewHeaderFooterView, XIB, Colors, alpha channel

so-logo-320x320SO-15604900 (Download Swift Recipe)

Use  background view to control appearance.

Special Instructions: Use `UITableViewHeaderFooterView` does not respond well to `backgroundColor`, `contentView.backgroundColor` or `tintColor`.


Scroll Edit Text without listening to Keyboard Notifications

so-logo-320x320SO-14465447 (Download Swift Recipe)

Use exactly 0 lines of code to handle keyboard notifications.

Special Instructions: Use `UITableViewController` to handle automatic scrolling of text editing views, no notifications!



Xavier Schott

0010 0000 years of algorithm crafting, software architecture, and bringing visionary mobile apps to market.