Post

Setting up UIKit programmatically with Objective-C

UIKit project set up with Objective-C and Programmatic UI in iOS 18.

Setting up UIKit programmatically with Objective-C

Hi there! Most probably you will be thinking, why would I need to setup a new iOS project using UIKit when there is SwiftUI, and most of all, configured with Objective-C!

Well, the thing is that UIKit is far from dead, and there’s a high chance you will encounter ObjC in the wild. Or you just wanna learn something new. Here, we are gonna setup a pretty basic UIKit project built from scratch with ObjC that will just show a UIViewController’s view background set to blue, using programmatic-ui instead of the default storyboard setup.

You can see a similar setup using Swift on this post.

Project set up

Go ahead and create a new project on XCode. Select iOS, App template, pick Storyboard as interface and good old Objective-C as language.

The project set up would look something like this

XCode Project

Build and run the project to check that everything works.

Storyboards cleanup

Now that everything is setup, we need to get rid of that dreaded Main.storyboard and setup our application to use programmatic UIKit.

Delete and move to the trash Main.storyboard.

With our Main.storyboard gone, it’s time to also remove its references within the project.

Select the project and go to the Build Settings. Under the Info.plist Values section, select and delete the property named UIKit Main Storyboard File Base Name, just like shown in the following image:

XCode Project

Next, we’re going to remove the reference to Main.storyboard from our Scene Configuration. Go to the Info tab just to the left of the previous Build Settings tab.

Under the Application Scene Manifest > Scene Configuration > Window Application Session Role > Item 0 (Default Configuration), select and delete the Storyboard Name property, as shown in the following image:

XCode Project

Programmatic-UI set up

Now that the Main.storyboard references are gone, it’s time to setup our programmatic UIKit setup.

Let’s start by setting the backgroundColor of our default ViewController’s view to UIColor.cyanColor within the viewDidLoad method. This will give us a visual guide and help us make sure that our changes work.

1
2
3
4
5
- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = UIColor.cyanColor;
}

Next, we’re going to update our SceneDelage scene:willConnectToSession:options method in order to set our an instance of our ViewController as the window’s rootViewController.

1
2
3
4
5
6
7
8
9
// Don't forget to import the header at the top of the file
#import "ViewController.h"

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    self.window = [[UIWindow alloc] initWithWindowScene:(UIWindowScene *)scene];
    self.window.rootViewController = [[ViewController alloc] init];

    [self.window makeKeyAndVisible];
}

We just finished. Go ahead and run the project, you should see our beautiful displayed ViewController’s cyan background in the simulator.

Simulator UIViewController’s background color set to UIColor.cyanColor

If you want to take a deeper look, here’s the GitHub repository with everything we have covered so far.

This post is licensed under CC BY 4.0 by the author.

Trending Tags