Creating a Custom ProgressBar with Three Information in Objective-C for iOS
In this tutorial, we will explore how to create a custom progress bar that displays three types of information: the number of slides remaining, the percentage of time used, and the percentage of time left. We’ll use Objective-C for this example as it’s commonly used for developing iOS applications.
Introduction to Customizing UI Elements
When working with user interface elements in iOS development, often we come across scenarios where standard controls don’t suffice or need further customization. In such cases, creating a custom view can be beneficial. In our case, the UIProgressBar class doesn’t support displaying multiple types of information at once, so we’ll create our own custom progress bar that meets our requirements.
Understanding the Progress Bar Requirements
Before diving into implementation, let’s break down what we need from our custom progress bar:
- Three pieces of information to be displayed: number of slides remaining, percentage of time used, and percentage of time left.
- Each piece of information should be represented by a different color:
- Blue for the number of slides remaining
- Green for the number of slides ahead (percentage of presentation time)
- Yellow for the number of time left (percentage of remaining time)
Creating the Custom Progress Bar
To create our custom progress bar, we’ll follow these steps:
Step 1: Create a New XIB File
First, let’s create a new XIB file to represent our custom progress bar. We can do this by opening the Xcode project navigator and clicking on the “File” menu, then selecting “New” > “File…” > “User Interface” (or use the shortcut Command + N). Choose a location for your new XIB file and click “Create”.
Step 2: Design the Custom Progress Bar
Open the newly created XIB file in Xcode. You’ll see an empty canvas where you can design your custom progress bar. To create our custom view, we’ll use a UIView
. Drag and drop this view onto the canvas.
Next, we need to add three UILabel
instances to display each piece of information:
- For slides remaining
- For time used (as a percentage)
- For time left (as a percentage)
Drag these labels onto the canvas as well. Name them something like “slidesRemaining”, “timeUsedPercentage”, and “timeLeftPercentage”.
Step 3: Add Colors to Our Progress Bar
To add colors to our progress bar, we’ll use the UIColor
class for each color.
For this example, let’s define these colors in our XIB file:
- Blue for time used
- Green for slides ahead (time used)
- Yellow for time left
You can do this by dragging a new Color Picker
control onto your canvas and configuring it to display the required color. Name the corresponding label for each color.
Implementing the Custom Progress Bar Logic
Now that we have our custom progress bar designed in XIB, let’s implement the logic to update the text of these labels.
We’ll create a new Swift file (e.g., “ProgressBar.swift”) and add our custom view implementation:
import UIKit
class ProgressBar: UIView {
// Labels for displaying information
let slidesRemainingLabel = UILabel()
let timeUsedPercentageLabel = UILabel()
let timeLeftPercentageLabel = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)
setupLabels()
setupColors()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// Setup labels for displaying information
private func setupLabels() {
slidesRemainingLabel.font = UIFont.systemFont(ofSize: 12)
timeUsedPercentageLabel.font = UIFont.systemFont(ofSize: 12)
timeLeftPercentageLabel.font = UIFont.systemFont(ofSize: 12)
addSubview(slidesRemainingLabel)
addSubview(timeUsedPercentageLabel)
addSubview(timeLeftPercentageLabel)
}
// Setup colors for the labels
private func setupColors() {
slidesRemainingLabel.textColor = UIColor.blue
timeUsedPercentageLabel.textColor = UIColor.green
timeLeftPercentageLabel.textColor = UIColor.yellow
}
// Update progress bar information
func updateProgressBar(slidesRemaining: Int, timeUsed: Double, timeLeft: Double) {
slidesRemainingLabel.text = "Slides Remaining: \(slidesRemaining)"
timeUsedPercentageLabel.text = "Time Used: \(String(format: "%.2f", timeUsed))%"
timeLeftPercentageLabel.text = "Time Left: \(String(format: "%.2f", timeLeft))%"
// Calculate colors based on current percentages
let totalSlides = 100
let timeUsedPercent = Float(slidesRemaining) / Double(totalSlides)
let timeLeftPercent = 1 - Float(timeUsedPercent)
slidesRemainingLabel.backgroundColor = UIColor.blue.withAlphaComponent(Int(255 * timeUsedPercent))
timeUsedPercentageLabel.backgroundColor = UIColor.green.withAlphaComponent(Int(255 * timeUsedPercent))
timeLeftPercentageLabel.backgroundColor = UIColor.yellow.withAlphaComponent(Int(255 * timeLeftPercent))
// Ensure labels are centered horizontally
slidesRemainingLabel.textAlignment = .center
timeUsedPercentageLabel.textAlignment = .center
timeLeftPercentageLabel.textAlignment = .center
}
}
This implementation includes methods to update the progress bar information and configure colors based on current percentages.
Integrate with Your iOS App
Now, let’s integrate our custom progress bar into your existing iOS app. You can add a ProgressBar
instance to any view controller in your app where you want to display the progress bar:
import UIKit
class ViewController: UIViewController {
// Create a new ProgressBar instance
var progressBar: ProgressBar!
override func viewDidLoad() {
super.viewDidLoad()
setupProgressBar()
}
private func setupProgressBar() {
let progressBarFrame = CGRect(x: 0, y: 0, width: self.view.bounds.width - 50, height: 40)
progressBar = ProgressBar(frame: progressBarFrame)
// Add labels and colors to the progress bar
for (index, label) in [slidesRemainingLabel, timeUsedPercentageLabel, timeLeftPercentageLabel].enumerated() {
if index < 2 { // First two are color-related; third is slides remaining
let color = getProgressColor(index)
label.backgroundColor = color
}
}
// Add to the view hierarchy
self.view.addSubview(progressBar)
// Update progress bar information
updateProgressBar(slidesRemaining: 50, timeUsed: 0.5, timeLeft: 0.3)
}
private func getProgressColor(_ index: Int) -> UIColor {
let totalLabels = 3
return UIColor(red: CGFloat(index) / Double(totalLabels), green: 1.0, blue: 1.0, alpha: 1.0)
}
}
In this code snippet, we’re creating a new ProgressBar
instance and setting up its frame to match our XIB file’s constraints.
Usage Example
Here is an example of how you can use your custom progress bar:
class ViewController: UIViewController {
// Create a new ProgressBar instance
var progressBar: ProgressBar!
override func viewDidLoad() {
super.viewDidLoad()
setupProgressBar()
}
private func setupProgressBar() {
let progressBarFrame = CGRect(x: 0, y: 0, width: self.view.bounds.width - 50, height: 40)
progressBar = ProgressBar(frame: progressBarFrame)
// Add labels and colors to the progress bar
for (index, label) in [slidesRemainingLabel, timeUsedPercentageLabel, timeLeftPercentageLabel].enumerated() {
if index < 2 { // First two are color-related; third is slides remaining
let color = getProgressColor(index)
label.backgroundColor = color
}
}
// Add to the view hierarchy
self.view.addSubview(progressBar)
// Update progress bar information
updateProgressBar(slidesRemaining: 50, timeUsed: 0.5, timeLeft: 0.3)
}
private func getProgressColor(_ index: Int) -> UIColor {
let totalLabels = 3
return UIColor(red: CGFloat(index) / Double(totalLabels), green: 1.0, blue: 1.0, alpha: 1.0)
}
}
// Function to update progress bar information
func updateProgressBar(slidesRemaining: Int, timeUsed: Double, timeLeft: Double) {
progressBar.updateProgressBar(slidesRemaining: slidesRemaining, timeUsed: timeUsed, timeLeft: timeLeft)
}
In this usage example, we’re updating the ProgressBar
instance with different progress bar values.
Conclusion
You now have a fully functional custom progress bar in your iOS app. This code snippet should give you a solid foundation for creating similar progress bars or modifying existing ones to fit your needs.
I hope that was informative and helpful! If you have any questions, feel free to ask me anytime.
Last modified on 2024-03-16