Swift

In this short Swift code example, you will learn how to check if file exists at specified path. Find a Documents directory on device Check if file exists at specified file path let fileNameToDelete = “myFileName.txt” var filePath = “” // Fine documents directory on device let dirs : [String] = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.allDomainsMask, true) if…

Read More Check if a File Exist

In this short Swift code example, you will learn how to perform a time consuming task in background thread in Swift, to keep mobile app responsive to user actions while the task is being executed and how to update UI from an async task once background task has completed. Do some task in background thread…

Read More Time Consuming Task in Background Thread in Swift

In this Swift code example, you will learn how to: Create UIBarButtonItem programmatically, How to set right side bar button item(rightBarButtonItem), How to set left side bar button item(leftBarButtonItem), How to set target action on UIBarButtonItem to call a custom function when the button is tapped. Below is a very simple code example in Swift…

Read More Create UIBarButtonItem Programmatically

In this short Swift code example, you will learn how to create UITextField in Swift programmatically. The code example below will cover the following: Create UITextField of specific height and width, Position UITextField at the center of the view, Set UITextField backgroundColor, Set UITextField textColor, Set UITextField UITextBorderStyle, Add UITextField as a subview, Set UITextField…

Read More Create UITextField Programmatically

In this short Swift code example, you will learn how to: Find a Documents directory on iOS device in Swift, Write a String value into a file in the Documents directory on an iOS device. let fileName = “myFileName.txt” var filePath = “” // Fine documents directory on device let dirs:[String] = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.allDomainsMask, true)…

Read More Write String Value Into a File in Swift

In this short Swift code example, you will learn how to declare a function that can take variadic parameters. We use a variadic parameter to specify that the parameter can be passed a varying number of input values when the function is called. The below code example will cover: Declare a function with variadic parameters,…

Read More Declare a function with variadic parameters in Swift

In this Swift code example, you will learn how to create UIImageView programmatically and how to load an image from a remote URL. Create UIImageView and UIImage programmatically, Load image data from a remote URL, Learn how to use dispatch_get_global_queue to start a background thread, Learn how to use dispatch_get_main_queue to update UI when the…

Read More UIImageView and UIImage. Load Image From Remote URL.

In this Swift code example, you will learn how to create UITabBarController programmatically. We will first create two View Controllers which will be Tab 1 and Tab 2, and then we will create one more View Controller which will serve as UITabBarController and will hold the two tabs. When a user taps on tab 1,…

Read More Create UITabBarController programmatically

In this Swift code example, you will learn how to create and display UICollectionView in Swift Programmatically. The Swift code example in this tutorial will cover the following: Create UICollectionView programmatically, Set UICollectionView layout UIEdgeInsets, Set UICollectionView item size(width and height), Set UICollectionView background color, Set UICollectionView Item background-color, Implement UICollectionView Delegate method didSelectItemAtIndexPath to…

Read More Create UICollectionView in Swift Programmatically

In this Swift code example, you will learn how to programmatically create UICollectionView and load a list of images from a remote URL. The below swift code example will cover: Create UICollectionView programmatically Set UICollectionView item size(width and height) Set UICollectionView background color Set UICollectionView Item background-color Implement UICollectionView Delegate method didSelectItemAtIndexPath to handle an…

Read More UICollectionView. Load List of Images From Remote Server Url

In this short Swift code example, you will learn how to determine main screen bounds as well as screen height and width in Swift. Get main screen bounds Get screen width Get screen height // Get main screen bounds let screenSize: CGRect = UIScreen.main.bounds let screenWidth = screenSize.width let screenHeight = screenSize.height print(“Screen width =…

Read More Determine Main Screen Height and Width

In this short Swift code example, you will learn how to determine device orientation in Swift. Determine device orientation when the view is about to appear Determine device orientation when device is being rotated to Landscape or Portrait import UIKit class ViewController: UIViewController { var textView:UITextView? override func viewDidLoad() { super.viewDidLoad() } override func viewWillAppear(_…

Read More Determine Device Orientation in Swift

In this short Swift code example, you will learn how to disable rotation of the UIViewController if device is rotated right or left. Note, that if your view is embedded into UINavigationController a different approach is needed. Check Swift Code Examples page to learn how to disable rotation of the view if it is embedded…

Read More Disable Rotation of UIViewController