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 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 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

In this short Swift code example, you will learn how to disable rotation of the UIViewController which is embedded into UINavigationController. We will need to override the shouldAutorotate function and return a value of false, Add to current UIViewController a UINavigationController extension. Disable Rotation of UIViewController Complete Code Example in Swift Below is a very simple…

Read More Disable Rotation of UIViewController Embedded Into UINavigationController

In this Swift code example, you will learn how to play the music mp3 file from a remote URL in Swift. The code example below will cover: Create AVPlayer and AVPlayerItem, Create UIButton programmatically, Create AVPlayerLayer and add it as a subview, Handle the Play button action to Pause and Play music, Set UIImage on…

Read More AVPlayer. Play Music MP3 File From a Remote URL In Swift.

In this Swift code example, you will learn how to add a playback slider to an AVPlayer, so that users can start playing music from any place they wish. The code example below will cover the following: Create AVPlayer and AVPlayerItem, Create UIButton programmatically, Create AVPlayerLayer and add it as a subview, Handle Play Button action…

Read More Add Playback Slider to AVPlayer. Example In Swift.

In this Swift code example, you will learn how to determine user’s current location using CLLocationManager and CLLocationManagerDelegate. The code example below will cover: Request user’s permission for your app to always be able to read user’s location. Optionality you could use requestWhenInUseAuthorization() instead of requestAlwaysAuthorization(), Update projects Info.plist with String values for NSLocationAlwaysUsageDescription and…

Read More Determine User’s Current Location Example in Swift

Error handling is the process of responding to and recovering from error conditions in your mobile app. Some operations aren’t guaranteed to always complete execution or produce a useful output. When an operation fails, it’s often useful to understand what caused the failure, so that your code can respond accordingly. Handle Error with Do, Try…

Read More Error Handling Example in Swift

In this short Swift code example, you will learn how to compare two strings despite their case sensitivity. To do that we will use the caseInsensitiveCompare() function and the ComparisonResult enumeration. Using caseInsensitiveCompare() and ComparisonResult let valueExpected = “SUCCESS” let valueProvided = “success” if valueExpected.caseInsensitiveCompare(valueProvided) == ComparisonResult.orderedSame { print(“Strings are equal”) } In the code example above, the caseInsensitiveCompare…

Read More Case Insensitive Comparison of Strings in Swift