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

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.


Select Single View Application and click on next.


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


Now, open Info.plist

Right click => Add row.


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

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


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 an image. Drag and drop the UIImageView. Select button apply constraint as shown in the image, then change ImageView content mode to ‘Aspect Fill’


Hold option key and select ViewController.swift

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

Now, Hold ctrl key + drag from UIImageView to ViewController class in order to create a property of UImageView. Enter the name as ‘ImageView’ and make sure the connection type is ‘Outlet’ and type is ‘UIImageView’, then click ‘Connect’. Again, Hold ctrl key + drag from UIButton to ViewController class in-order to create a method of UIButton.

Enter the name “btnSelectImageDidTap”, Make sure connection type is “Action” and type is “UIButton”. The Event must be “Touch up inside”

Click “Connect”

This will create a method of UIButton named “btnSelectImageDidTap:”

This method will execute when a user taps on the button.

Now It will look like this.


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

Select Cocoa Touch Class click Next.


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


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.

import UIKit

class ViewController: UIViewController {

Step 1:- Create IBOutlet Of ImageView

    //MARK: - IBOutlet Of ImageView
    @IBOutlet weak var imageView: UIImageView!

 Step 2:- Create Button Action For Fetch Image 

     //MARK: - Create Button Action For SelectImage From Gallery Or Camera
    @IBAction func btnSelectImage(_ sender: UIButton) {

        //MARK: - This class will be responsible for Showing image selection Options to user.
        HSImageSelection.manager.openImagePicker(from: self, allowEditing: true) { (imageSelectionManager, selectedImage) in
            if let image = selectedImage {
                self.imageView.image = image;

And done!

Let’s see the output:


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.

Grab a free copy of Reusable iOS Components Demo from Github.

Author Bio
Hitesh Trivedi
Hitesh Trivedi
Designation: iOS Team Lead

Hitesh Trivedi is an iOS Team Lead at Space-O Technologies. He has over 10 years of experience in iOS app development. He has guided to develop over 100 iPhone apps with unique features and functionalities. He has special expertise in Swift and Objective-C.