Talk With Angel Garcia!

Talk With Angel Garcia!

a blog about software and beers

Creating Xamarin.iOS views imperatively (without storyboards) with iOS 13 [Breaking Changes]

Today I was playing with Xamarin iOS and decided to follow the Introduction to Xamarin.iOS module at MS Learn, before starting the tutorial I updated my VS for Mac 2019 and XCode (11.2) to the latest versions so I can start using the iOS 13’s features.

First thing I noticed was this new file called SceneDelegate.cs, and guess what… I ignored it.

Single View Xamarin iOS Project

Then I tried to replace the default main view (Main.storyboard) with a custom view from code, so I created a class for my view (MyViewController) that inherits from UIViewController and just added a blue background to this view.

public class MyViewController : UIViewController
{

public override void ViewDidLoad()
{
base.ViewDidLoad();
View.BackgroundColor = UIColor.Blue;
}

public override void DidReceiveMemoryWarning()
{
base.DidReceiveMemoryWarning();
// Release any cached data, images, etc that aren't in use.
}
}

If you follow the documentation you will see that you need to assign a UIWindow in the AppDelegate.cs by replacing the code in the method FinishedLaunching with the following code:

// Create a new window instance based on the screen size
Window = new UIWindow(UIScreen.MainScreen.Bounds);

// Assign the root view controller
Window.RootViewController = new MyViewController();

// Make the window visible
Window.MakeKeyAndVisible();

return true;

Then we need to modify the Info.plist in order to remove the Main storyboard from the Main Interface property under the Deployment Info

Leave this value empty

And the last thing to do was remove the file Main.storyboard since we are using our ViewController to make the UI.

If we run the app, after following the docs… BOOM! you get this error:

Objective-C exception thrown. Name: NSInvalidArgumentException Reason: Could not find a storyboard named ‘Main’ in bundle NSBundle

And here I realized that something was missing in the documentation for iOS 13 getting started.

First let’s get out of this error by editing the Info.plist (I used VS Code) file to find the property UISceneStoryboardFile and delete it with the value.

Rebuild the project after editing this file

Voilà our project runs again! But we don’t see the blue background of our view, this is because the new changes in the way that iOS manages the app initialization using the SceneDelegate.cs.

With that being said let’s go back to the FinishedLaunching method in the AppDelegate.cs and put the initial default code (just a return true), and let’s move this logic (with a small change) to the WillConnect method in the SceneDelegate.cs:

// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see UIApplicationDelegate `GetConfiguration` instead).
// Create a new window instance based on the screen size
Window = new UIWindow(UIScreen.MainScreen.Bounds);
//Assign the current scene to the Window instance [NEW CODE NEEDED]
Window.WindowScene = (UIWindowScene)scene;
// Assign the root view controller
Window.RootViewController = new MyViewController();
// Make the window visible
Window.MakeKeyAndVisible();

That’s all folks!, now we can see our view with the blue background as the main view.

DROP A COMMENT

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.