【Swift】A guide to creating a simple iOS app without Storyboard using UIKit.

Simple Apps

I want to share a simple guide about creating a simple iOS app using only code in Swift.

I created this because I could not find a good one by searching for Code only”.

I am writing this one for peole who searched for the same and could not find it.
I hope it will be helpful.

Code Version

Xcode Version: 12.4
Swift Version: 5.3.2

Final Product

Here is the simple app we will create (it’s just a basic implementation with a button):

App Implementation Steps

Setting Up the Project

  1. Launch Xcode and select Create a new Xcode project.

Configuring Project Settings

  1. Choose a name for your app in Product Name and click Next to select the folder for saving.
    • Set Interface to Storyboard and Life Cycle to UIKit App Delegate.
    • You can check or uncheck Use Core Data based on your preference.

Deleting Main.storyboard

  • Remove Main.storyboard from the project in the navigator area.
  • This step is not required, but I think it adds clarity if we include this step.

Configuring Main Interface

  1. Select the app name in the navigator area (e.g., Test.APP).
  2. In the TARGETS section, go to General and clear the Main Interface field in Deployment Info.

Adjusting Info Settings

  • To configure info settings, expand the following menus and remove the specified items:
    1. In the TARGETS section, under General, select Custom iOS Target Properties.
    2. Navigate to Application Scene Manifest, then Scene Configuration, and delete Storyboard Name.

Configuring SceneDelegate.swift

Add the following code to SceneDelegate.swift:

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        //Unwrap UIwindwos 
        guard let windowScene = (scene as? UIWindowScene) else { return }
        
        //Setting WindwoScece in Self
        let window = UIWindow(windowScene:  windowScene)
        self.window = window
        
        //  rootView
        window.rootViewController = ViewController()
        //「window」を最前線に表示する
        window.makeKeyAndVisible()
    }

Configuring ViewController.swift

Finally, add the following code within the viewDidLoad method of ViewController.swift:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
        button.backgroundColor = .red
        button.setTitle("Test", for: .normal)
        view.addSubview(button)
        
    }
}

Testing in the Simulator

Run the app in the simulator to confirm it works as expected.

Final Thoughts

This is a very basic implementation, but I hope it serves as a helpful reference for someone out there. Until next time!

Comments

Copied title and URL