Question or problem with Swift language programming: I’m rebuilding an app without storyboards and the part of it that I’m having the most trouble with is navigating view-to-view programatically. Few things are written out there which don’t use storyboards, so finding an answer for this has been tough. My problem is pretty simple. No Filmmaker should be without this App I’ve had the iPad version for a while and I’ve been waiting for it on my Mac. I love making professional shot list with this app. But the schuedling portion is invaluable. You will know excatly how long your shoot will be to the minute. Twice, this app has saved me and my production. NiblessMenu is sample code written in Swift demonstrating how to programmatically create the main menu in a Mac app, without using a nib or storyboard. See the LICENSE.txt file for the software license agreement. To accommodate the screen sizes of different devices and environments, such as multitasking, use an Xcode storyboard to provide a launch screen. Storyboards are flexible and adaptable, and you can use a single storyboard to manage all of your launch screens. For developer guidance, see Responding to the Launch of Your App. No longer works in Swift 4. I spent hours and still no luck programmatically create a window with pure swift. Apple makes is so hard to do it (read no official getting started guide without storyboard ) – Kun Dec 21 '17 at 15:34.
-->This article covers creating a Xamarin.Mac application's user interface directly from C# code, without .storyboard files, .xib files, or Interface Builder.
Overview
Deleting apps from mac computer. When working with C# and .NET in a Xamarin.Mac application, you have access to the same user interface elements and tools that a developer working in Objective-C and Xcode does. Typically, when creating a Xamarin.Mac application, you'll use Xcode's Interface Builder with .storyboard or .xib files to create and maintain you application's user interface.
You also have the option of creating some or all of your Xamarin.Mac application's UI directly in C# code. In this article, we'll cover the basics of creating user interfaces and UI elements in C# code.
Switching a window to use code
When you create a new Xamarin.Mac Cocoa application, you get a standard blank, window by default. This windows is defined in a Main.storyboard (or traditionally a MainWindow.xib) file automatically included in the project. Mac run app as sudo. This also includes a ViewController.cs file that manages the app's main view (or again traditionally a MainWindow.cs and a MainWindowController.cs file).
To switch to a Xibless window for an application, do the following:
- Open the application that you want to stop using
.storyboard
or .xib files to define the user interface in Visual Studio for Mac. - In the Solution Pad, right-click on the Main.storyboard or MainWindow.xib file and select Remove:
- From the Remove Dialog, click the Delete button to remove the .storyboard or .xib completely from the project:
Now we'll need to modify the MainWindow.cs file to define the window's layout and modify the ViewController.cs or MainWindowController.cs file to create an instance of our
MainWindow
class since we are no longer using the .storyboard or .xib file.Modern Xamarin.Mac apps that use Storyboards for their user interface may not automatically include the MainWindow.cs, ViewController.cs or MainWindowController.cs files. As required, simply add a new Empty C# Class to the project (Add > New File.. > General > Empty Class) and name it the same as the missing file.
Defining the window in code
![Mac App Without Storyboards Mac App Without Storyboards](/uploads/1/3/4/1/134147142/329252532.jpg)
Next, edit the MainWindow.cs file and make it look like the following:
Let's discuss a few of the key elements.
First, we added a few Computed Properties that will act like outlets (as if the window was created in a .storyboard or .xib file):
These will give us access to the UI elements that we are going to display on the window. Since the window isn't being inflated from a .storyboard or .xib file, we need a way to instantiate it (as we'll see later in the
MainWindowController
class). That's what this new constructor method does:This is where we will design the layout of the window and place any UI elements needed to create the required user interface. Before we can add any UI elements to a window, it needs a Content View to contain the elements:
This creates a Content View that will fill the window. Now we add our first UI element, an
NSButton
, to the window:The first thing to note here is that, unlike iOS, macOS uses mathematical notation to define its window coordinate system. So the origin point is in the lower left hand corner of the window, with values increasing right and towards the upper right hand corner of the window. When we create the new
NSButton
, we take this into account as we define its position and size on screen.The
AutoresizingMask = NSViewResizingMask.MinYMargin
property tells the button that we want it to stay in the same location from the top of the window when the window is resized vertically. Again, this is required because (0,0) is at the bottom left of the window.Finally, the
ContentView.AddSubview (ClickMeButton)
method adds the NSButton
to the Content View so that it will be displayed on screen when the application is run and the window displayed.Next a label is added to the window that will display the number of times that the
NSButton
has been clicked:Since macOS doesn't have a specific Label UI element, we've added a specially styled, non-editable
NSTextField
to act as a Label. Just like the button before, the size and location takes into account that (0,0) is at the bottom left of the window. The AutoresizingMask = NSViewResizingMask.WidthSizable | NSViewResizingMask.MinYMargin
property is using the or operator to combine two NSViewResizingMask
features. This will make the label stay in the same location from the top of the window when the window is resized vertically and shrink and grow in width as the window is resized horizontally.Mobile App Storyboard
Again, the
ContentView.AddSubview (ClickMeLabel)
method adds the NSTextField
to the Content View so that it will be displayed on screen when the application is run and the window opened.Adjusting the window controller
Since the design of the
MainWindow
is no longer being loaded from a .storyboard or .xib file, we'll need to make some adjustments to the window controller. Edit the MainWindowController.cs file and make it look like the following:Let discuss the key elements of this modification.
First, we define a new instance of the
MainWindow
class and assign it to the base window controller's Window
property:We define the location of the window of screen with a
CGRect
. Just like the coordinate system of the window, the screen defines (0,0) as the lower left hand corner. Next, we define the style of the window by using the Or operator to combine two or more NSWindowStyle
features:https://renewen195.weebly.com/blog/homebank-app-for-mac. The following
NSWindowStyle
features are available:- Borderless - The window will have no border.
- Titled - The window will have a title bar.
- Closable - The window has a Close Button and can be closed.
- Miniaturizable - The window has a Miniaturize Button and can be minimized.
- Resizable - The window will have a Resize Button and be resizable.
- Utility - The window is a Utility style window (panel).
- DocModal - If the window is a Panel, it will be Document Modal instead of System Modal.
- NonactivatingPanel - If the window is a Panel, it will not be made the main window.
- TexturedBackground - The window will have a textured background.
- Unscaled - The window will not be scaled.
- UnifiedTitleAndToolbar - The window's title and toolbar areas will be joined.
- Hud - The window will be displayed as a heads-up display Panel.
- FullScreenWindow - The window can enter full screen mode.
- FullSizeContentView - The window's content view is behind the title and toolbar Area.
The last two properties define the Buffering Type for the window and if drawing of the window will be deferred. For more information on
NSWindows
, please see Apple's Introduction to Windows documentation.Finally, since the window isn't being inflated from a .storyboard or .xib file, we need to simulate it in our MainWindowController.cs by calling the windows
AwakeFromNib
method:This will allow you to code against the window just like a standard window loaded from a .storyboard or .xib file.
Displaying the window
With the .storyboard or .xib file removed and the MainWindow.cs and MainWindowController.cs files modified, you'll be using the window just as you would any normal window that had been created in Xcode's Interface Builder with a .xib file.
The following will create a new instance of the window and its controller and display the window on screen:
At this point, if the application is run and the button clicked a couple of times, the following will be displayed:
Adding a code only window
If we want to add a code only, xibless window to an existing Xamarin.Mac application, right-click on the project in the Solution Pad and select Add > New File.. In the New File dialog choose Xamarin.Mac > Cocoa Window with Controller, as illustrated below:
Best Storyboard App
Just like before, we'll delete the default .storyboard or .xib file from the project (in this case SecondWindow.xib) and follow the steps in the Switching a Window to use Code section above to cover the window's definition to code.
Adding a UI element to a window in code
Whether a window was created in code or loaded from a .storyboard or .xib file, there might be times where we want to add a UI element to a window from code. For example:
The above code creates a new
NSButton
and adds it to the MyWindow
window instance for display. Basically any UI element that can be defined in Xcode's Interface Builder in a .storyboard or .xib file can be created in code and displayed in a window.Defining the menu bar in code
Because of current limitations in Xamarin.Mac, it is not suggested that you create your Xamarin.Mac application's Menu Bar–
NSMenuBar
–in code but continue to use the Main.storyboard or MainMenu.xib file to define it. That said, you can add and remove Menus and Menu Items in C# code.For example, edit the AppDelegate.cs file and make the
DidFinishLaunching
method look like the following:Free Storyboarding App
The above creates a Status Bar menu from code and displays it when the application is launched. For more information on working with Menus, please see our Menus documentation.
Summary
This article has taken a detailed look at creating a Xamarin.Mac application's user interface in C# code as opposed to using Xcode's Interface Builder with .storyboard or .xib files.