Back To Blog

How to Create Reusable iOS Components in iPhone App To Reduce Code Complexity

ios-components

Reusable iOS components are extremely useful when you’re required to add same feature or component in different parts of your mobile app. In fact, most developers create reusable iOS components for future use.

For instance, if you’ve a repeating component, it can be time consuming to configure them in each places of your iPhone app and make it look right in context. So, to make the process smoother, developer create reusable iOS components to eliminate the code complexity.

Today, we’ll build ios app to demonstrate how to create such reusable iOS components in iphone apps.

Let’s Get Started

Open XCode and create a new project.

2

Select Single View Application and click on next.

3

In the next tab, enter your app name and details.

4

Now, open Info.plist

Right click => Add row.

5_1

Key: NSPhotoLibraryUsageDescription
Value: We are using photo library to pick photo

Key: NSCameraUsageDescription
Value: We are using camera to click photo

5_2

Next, open Main.storyboard. Find your view controller and drag & drop UIButton.

change button title to ‘select image’. Now select button and apply constraint as shown in image.

6

Now, drag and drop the UIImageView. Select button apply constraint as shown in image.

7_1

Next, change ImageView contentmode to ‘Aspect Fill’

7_2

Hold option key and select ViewController.swift

This will open side by side Assistant editor shown in image.

8

Now, Hold ctrl key + drag from UIImageView to ViewController class in-order to create property of UImageView

9

Enter name as ‘ImageView’ and make sure the connection type is ‘Outlet’ and type is ‘UIImageView’, then click ‘Connect’.

10

Again, Hold ctrl key + drag from UIButton to ViewController class in-order to create method of UIButton.

11

Enter name “btnSelectImageDidTap”

Make sure connection type is “Action” and type is “UIButton”

Event must be “Touch up inside”

Click “Connect”

This will create method of UIButton named “btnSelectImageDidTap:”

This method will execute when user tap on the button.

12

Now It will look like this.

13

Goto File>New>File (command+N).

Select Cocoa Touch Class click Next.

14_1

Enter class name “HSImageSelection” subclass of NSObject click Next.

14_2

Select desired location to save new class and click Create.

This will create new class named “HSImageSelection”.

This class will be responsible for Showing image selection Options to user.

Replace code of new class HSImageSelection with the same class from this project.

Also, replace code of btnSelectImageDidTap: method of class ViewController with this code.

16

And done!

Let’s see the output:

finaloutput

While this solution looks pretty simple, but the fact is, the complexity of putting in the right context may arise depending on the app. So, to get the best results and develop an efficient mobile app, talk to the experts or consult with an iPhone app development company to ensure implementation job is done correctly.

 
0
Shares
 

Have an iOS App Idea in Mind? Let's Discuss!

Nick-100x100


‘ Eugene, Vlad, Jigar and their respective teams have been fantastic on all the various apps they have worked on and developed for us over the past 18 months. They are always proactive, responsive to questions and consistently provide top quality development and design work. The use of Skype, email and their own project management tool also makes it easy to keep track on how a project is progressing. I would highly recommend Space-O Technologies for any iOS development or design. ’ Nick Power