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.
Value: We are using photo library to pick photo
Value: We are using 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 image.
Now, drag and drop the UIImageView. Select button apply constraint as shown in image.
Next, change ImageView contentmode 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 property of UImageView
Enter 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 method of UIButton.
Enter name “btnSelectImageDidTap”
Make sure connection type is “Action” and type is “UIButton”
Event must be “Touch up inside”
This will create method of UIButton named “btnSelectImageDidTap:”
This method will execute when user tap 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.
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.