Creating a custom native control using Xamarin.iOS

A lot of apps need more customized UI than what is available with the current set of controls. Today we’ll add a customized Check Box control since iOS does not have a Check Box. Some suggest that you use the Switch control as a replacement for the Check Box, but when developing custom apps and the client or boss want a check box, then I tend to implement a check box while others say it cannot be done. I’ll show the basics of creating a custom control for iOS of a check box.

Right click on your Xamarin iOS project and select Add Item.

Creating a Custom Native Control by Jeff Wyzard

In the Add New Item Window, Select “UIView” and give it a name for your custom control. After giving your control a name, click the Add button so Visual Studio add the file(s) to your project.

We need to modify the register attribute on the class to add DesignTimeVisible(true)

Creating a Custom Native Control by Jeff Wyzard

Now we will add any properties that we’ll need for customizing our new control and we’ll use the Visual Studio code snippet “propfull”. In the class, type propfull and hit the tab key twice.

Creating a Custom Native Control by Jeff Wyzard

Visual Studio will add some code to your class with default type of int and named “MyProperty” with the property type, name, name of the class and default value highlighted

Creating a Custom Native Control by Jeff Wyzard

You can change the different highlighted options and press the tab key to go from one highlighted option to another. In my case, I wanted a Boolean property named “IsChecked” with a default false as the default value.

Creating a Custom Native Control by Jeff Wyzard

We need to add an attribute Export with the property name and the property will be browsable.

Creating a Custom Native Control by Jeff Wyzard

Now we can start drawing portion of the custom control. Type override and select the Draw method with the CGRect argument since we’ll need to override the Draw method for the drawing.

Creating a Custom Native Control by Jeff Wyzard

Next in the Draw, we can draw the box for our check box control.

Creating a Custom Native Control by Jeff Wyzard

You can do whatever you need to do in the Draw method to create your custom control.

Have fun!

Follow me online to learn about creating Xamarin Forms custom view that uses these custom native controls.
My Blog: www.wyzard.us
LinkedIn: https://www.linkedin.com/in/jeff-wyzard-b89a7011