Ticker

6/recent/ticker-posts

How to create a stored property in an swift extension



There are two ways of adding stored properties to the Swift extensions. To understand this, I'm taking a simple example to explain it in step by step.


 Example Abstract: Create a floating text field placeholder to an UITextField.

 Solution:

            To create a Floating text filed placeholder, we need an extra UILabel object to replace the default placeholder text of UITextField and add that UILabel as sub-view to our UITextField class. Once you add the UILabel, add move up and move down animation to that Label object accordingly.

 Let's follow the below steps to fix this -

 1) Let's create a new project

 2) Under the project, create a new swift file with FloatingTextField.swift

 3) To handle the floating action and placeholder text color, I'm going to add two extra stored properties to the existing UITextField class with its extension.

 Option - 1:

           Add the below piece of code to your extension and compile, you will be able to see the compiled code with no error. The below code uses a simple methodology of swift structures to set and get the values to a stored property.


 Option - 2:

           Add the below piece of code to your extension and compile, you will be able to see the compiled code with no error. The below code uses the Objective-C API to set and get the values to a stored property.



 4) To complete our example, add the below helper function to your extension, and run the project.

Post a Comment

0 Comments