Nandha
Nandha

Reputation: 47

Xamarin.iOS Custom View with class file VS2019 Version 16.9

Can anyone please guide me how to create a custom view in Xamarin.iOS using XCode.

enter image description here

Im trying to create a Custom Circular View with the class file. I was able to create a XIB file but not able to create a class file.

Upvotes: 0

Views: 172

Answers (1)

Khaled Gaber
Khaled Gaber

Reputation: 121

To continue on what you did, do the following.

  1. Create an empty class and name it CircularView

     public partial class CircularView : UIView
     {
         public static readonly NSString Key = new NSString("CircularView");
         public static readonly UINib Nib;
    
         static CircularView()
         {
             Nib = UINib.FromName("CircularView", NSBundle.MainBundle);
         }
    
         protected CircularView(IntPtr handle) : base(handle)
         {
             // Note: this .ctor should not contain any initialization logic.
         }
    
         public static CircularView CreateView()
         {
             return (CircularView)Nib.Instantiate(null, null)[0];
         }
     }
    
  2. Create another class and name it CircularView.designer.cs

    [Register ("CircularView")]
     partial class CircularView
     {
        void ReleaseDesignerOutlets ()
        {
                    
        }
     }
  1. Edit project file and add DependUpon tags like below

BEFORE

<Compile Include="Controls\CircularView.cs" />
<Compile Include="Controls\CircularView.designer.cs" />

AFTER

<Compile Include="Controls\CircularView.cs" />
<Compile Include="Controls\CircularView.designer.cs" >
    <DependentUpon>CircularView.cs</DependentUpon>
</Compile>

this will ensure that VS shows the designer file as a child of CircularView.cs

  1. where you want to use the new Custom view do the following
   var v = CircularView.CreateView();
   vwCustom.Add(v);

where vwCustom is a normal UIView added from Designer in my view controller, you can ofcourse name it anything.

Please let me know if you need further help.

Upvotes: 1

Related Questions