Found 517 Articles for Swift

Array extension to remove an object by value in Swift

Nitin Aggarwal
Updated on 04-Apr-2023 10:49:30

610 Views

To remove an object by value from an array in Swift, you can create an extension to the Array type. You can then call the function that takes the value to eliminate as a parameter. In this article, we will use the filter function to remove an object passing by value in the Swift language. Let's see some examples of how you can eliminate an object by value from an array Example 1: Remove an Object Using the Filter Function You can use the built-in filter method to create a new array without the matching value. Algorithm Step 1 ... Read More

How to dismiss ViewController in Swift?

Nitin Aggarwal
Updated on 27-Mar-2023 15:48:51

3K+ Views

In Swift, we have a dismiss method of the class UIViewController that can be used to dismiss a ViewController in Swift. In this method, a Boolean value is used as an argument. There is an argument in this argument that asks whether the dismissed controller should be animated. By default, this is true in this method. The following example shows how to dismiss a UIViewController screen in Swift. First view controller setup In this step, we will set up the first view controller to present the second view controller. We will add a button to the first view controller, which ... Read More

How to assign an action to the UIImageView object in Swift?

Nitin Aggarwal
Updated on 11-Apr-2023 10:46:08

1K+ Views

In the iOS applications, the UIImageView class does not provide in-build support to make it clickable like other components e.g. UIButton. In order to make UIImageView clickable, you can add a UITapGestureRecognizer to the image view. Remember that, by default, UIImageView does not take any interaction from the user. To make it interactable, set true to the isUserInteractionEnabled property. In this article, you will learn how you can add a tag gesture to the image view in Swift. To add a tap gesture, we will follow these steps − Step 1 - Create an Image View let profileImageView = ... Read More

How to Apply Gradient to the background view of the iOS Swift App?

Nitin Aggarwal
Updated on 27-Mar-2023 15:46:00

7K+ Views

In iOS, there is a class CAGradientLayer to apply gradient colors to views. In this article, we will look at how you can use the CAGradientLayer class to apply gradients to the background of a view in iOS. The CAGradientLayer class provides different properties like colors, locations, points, frame, etc. We will use them to apply the gradient to a view. In last, we will use the insertSublayer method to add the gradient layer to the super view. To apply a gradient to the background view of an iOS Swift app, you can follow these steps − Step 1 - ... Read More

Get input value from TextField in iOS alert in Swift

Nitin Aggarwal
Updated on 27-Mar-2023 15:37:45

1K+ Views

Generally, we use UIAlertController to show an alert with a dismiss action button. But UIAlertController provides you with more flexibility to add UITextFields to the alert. Let's see some examples of different use cases. When you add a text field to the UIAlertController, you can take the input value entered in the text field. It is also possible to add multiple text fields. In this example, we will use the following steps to get input values from the text field in iOS. Basic Setup In this step, you will add a button object to the controller's view to present the ... Read More

Move the text field when the keyboard appears in Swift

Nitin Aggarwal
Updated on 27-Mar-2023 15:34:04

2K+ Views

In real iOS applications, most of the time you deal with UITextFields for taking various inputs. In order to make the text fields visible while editing, you can manage the constraints by updating them. To manage the constraints, you have to add observers for keyboard showing and hiding. In this article, we will move the text field when the keyboard appears with the following steps. Step 1 - Basic setup In this step, we will do some basic setup by adding a text field to enter the email address. We will add the text field at the bottom of the ... Read More

How to set back button text in Swift?

Nitin Aggarwal
Updated on 27-Mar-2023 15:27:46

1K+ Views

By default, a view controller shows the text "Back" with an arrow on the back button in iOS. But you can set a custom title and icon for the back button item. Let's see an example of how to set custom back button text in Swift. In this example, we will set up two different view controllers to see the default behavior of the back button and how to set the custom back button text. First View Controller Setup In this step, we will set up the first view controller to push the second view controller. Here is the code ... Read More

How to stop unwanted UIButton animation on title change?

Nitin Aggarwal
Updated on 27-Mar-2023 15:29:57

550 Views

In iOS applications, you will often need to customize the button according to your requirements. When you add a button to the parent view, the button title is animated by default. But if you want to stop that default animation, you can use the custom button in UIKit. We will first create a situation where you can see this problem. Here is the basic setup. Initially, we will add a register button to the parent view with basic customization. We will add the constraints programmatically to the button to set the button's position. Step 1 − Create a button ... Read More

Swift Program to get the subarray from an array using a specified range of indices

Ankita Saini
Updated on 19-Jul-2023 14:14:13

853 Views

In swift, we have various methods like Array() initializer and for-in loop that can be used for getting the subarray from an array using a specified range of indices. An array stores elements of the same data type in an order. Let’s understand both the methods in detail in this article. Method 1: Using Array() initialiser To get the subarray from an array using a specified range of indices we uses Array() initialiser. Here we find the subarray using range operator and then convert the elements into the array using Array() initialiser. Algorithm Step 1 − Create an ... Read More

Swift Program to get the flattened 1D array

Ankita Saini
Updated on 19-Jul-2023 14:19:52

321 Views

Swift has various functions like flatmap(), compactmap(), reduce() to flatten an 1D array. An array is used to store elements of same data type in an order whereas a set is used to store distinct elements of same data type without any definite order. Method 1: Using flatmap() Function To flatten 2D array into 1D array we can use flatmap() function. The flatmap() function return an array by concatenating the sub arrays into a single array. Syntax func flatMap{_res} Here, res is a closure which accepts the element of the specified array as its argument and return a flattened ... Read More

Advertisements