In this tutorial, we’ll build a simple app called “QuickTabs” using UIKit, featuring a tab bar controller and a settings screen. This approach gives you more control over your app’s structure and is great for working in teams. Let’s get started!
Prerequisites
- Xcode installed on your Mac
- Basic knowledge of Swift and iOS development
Step 1: Project Setup
- Open Xcode and create a new iOS app project.
- Choose “App” as the template.
- Name your project “QuickTabs”.
- In the interface dropdown, select “Storyboard” (we’ll remove it later).
- Click “Create”.
Step 2: Remove Storyboard
- Delete the
Main.storyboard
file from your project. - In your project settings, go to the “Info” tab.
- Delete the “Main storyboard file base name” entry.
- In the “Deployment Info” section, clear the “Main Interface” field.
- Delete the Scene Delegate and Application Scene Manifest property under Info file as we are not using Scene Delegate or support for Multiple Windows.
Step 3: Set Up the App Delegate
Replace the contents of AppDelegate.swift
with the following code:
import UIKit @main class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { window = UIWindow(frame: UIScreen.main.bounds) window?.makeKeyAndVisible() let tabBarController = UITabBarController() let homeVC = HomeViewController() homeVC.tabBarItem = UITabBarItem(title: "Home", image: UIImage(systemName: "house"), tag: 0) let settingsVC = SettingsViewController() settingsVC.tabBarItem = UITabBarItem(title: "Settings", image: UIImage(systemName: "gear"), tag: 1) tabBarController.viewControllers = [homeVC, settingsVC] window?.rootViewController = tabBarController return true } }
This sets up our app’s main window and creates a tab bar controller with two tabs: Home and Settings.
Step 4: Create the Home View Controller
Create a new file called HomeViewController.swift
and add the following code:
import UIKit class HomeViewController: UIViewController { private let welcomeLabel: UILabel = { let label = UILabel() label.text = "Welcome to SuperApp!" label.font = UIFont.systemFont(ofSize: 24, weight: .bold) label.textAlignment = .center label.translatesAutoresizingMaskIntoConstraints = false return label }() override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white setupUI() } private func setupUI() { view.addSubview(welcomeLabel) NSLayoutConstraint.activate([ welcomeLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor), welcomeLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor) ]) } }
This creates a simple home screen with a welcome message.
Step 5: Create the Settings View Controller
Create another new file called SettingsViewController.swift
and add the following code:
import UIKit class SettingsViewController: UIViewController { private let settingsLabel: UILabel = { let label = UILabel() label.text = "Settings" label.font = UIFont.systemFont(ofSize: 24, weight: .bold) label.textAlignment = .center label.translatesAutoresizingMaskIntoConstraints = false return label }() private let darkModeSwitch: UISwitch = { let switch_ = UISwitch() switch_.translatesAutoresizingMaskIntoConstraints = false return switch_ }() override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white setupUI() } private func setupUI() { view.addSubview(settingsLabel) view.addSubview(darkModeSwitch) NSLayoutConstraint.activate([ settingsLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor), settingsLabel.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20), darkModeSwitch.topAnchor.constraint(equalTo: settingsLabel.bottomAnchor, constant: 20), darkModeSwitch.centerXAnchor.constraint(equalTo: view.centerXAnchor) ]) darkModeSwitch.addTarget(self, action: #selector(darkModeSwitchValueChanged), for: .valueChanged) } @objc private func darkModeSwitchValueChanged() { if darkModeSwitch.isOn { view.backgroundColor = .darkGray settingsLabel.textColor = .white } else { view.backgroundColor = .white settingsLabel.textColor = .black } } }
This creates a settings screen with a switch to toggle dark mode.
Step 6: Build and Run
You should now be able to build and run your app. You’ll see a tab bar with two options: Home and Settings. The Home tab will show your welcome message, and the Settings tab will have a switch to toggle dark mode.
Conclusion
Congratulations! You’ve built a simple iOS app programmatically using UIKit. This project has introduced you to several important concepts:
- Setting up an app without a storyboard
- Using UITabBarController for navigation
- Creating view controllers programmatically
- Using Auto Layout for UI design
- Implementing basic user interactions