Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8900975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:14:03+00:00 2026-06-15T01:14:03+00:00

Using MonoTouch, I’m trying to make a custom view, so I can reuse this

  • 0

Using MonoTouch, I’m trying to make a custom view, so I can reuse this view multiple times in a screen. (it’s a bunch of labels and buttons)
After reading the answer to this post:
How to add a custom view to a XIB file defined view in monotouch
I must be getting close, but this only seems to allow me to add a custom view, defined in in myCustomView.CS, but what I’m looking for is a way to use a separate .XIB to design my custom view in the interface builder and only use the corresponding cs-file to add logic.
If I try to make a XIB with the same name, the contents won’t become visible.

Followed steps:

  • In MyCustomView.CS I have put [Register("MyCustomView")] .

  • I implemented the constructor public MyView(IntPtr handle) : base(handle) {}

  • Open ViewController.XIB in Interface Builder, add a UIView and set it’s Class to MyCustomView.

When I run this code, I see the newly added UIView, but not the elements inside of the view, i.e. the contents of MyCustomView.XIB

If I add something to the code of MyCustomView.CS, like this.Backgroundcolor = UIColor.Blue; this is displayed, but any elements added to the .XIB are not. How do I tell Monotouch to display all the stuff defined inside MyCustomView.XIB?

In Objective-C samples I see stuff like “initWithNib”
Unfortunately the “LoadNib” section is not documented in in the Monotouch api, but that sounds like something I should do, but how and where?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:14:04+00:00Added an answer on June 15, 2026 at 1:14 am

    A very very nice question, but it is more about how XIBs work and what we can do about it.

    The issue is when you add an object to a XIB and set it to be of a custom class, you just instruct the UIKit to create an instance of this object. The fact you see in IB as a view, a button or a whateverish control is just how IB draws it for you. This way the responsibility for creating the outlook falls completely on your class.

    In Objective-C we can do a nice cheat to have XIBs with custom views and still add them to IB by setting the class name of the added view. This is possible because you can return any instance from the -init method of your class in Objective-C, which is not possible with C#.

    However, you can still ease your life by doing the following:

    1. Let’s define XIB to be a XIB which contains your custom view’s content; Placement to be a XIB where you want to add you custom view.
    2. Create you custom view class and a XIB to lay the internals of it
    3. Handle AwakeFromNib method to actually load your XIB
    4. After having loaded your XIB just add the internals of it to your instance as subviews.
    5. Put a regular view and set it class to your own into Placement
    6. Enjoy!

    You have, however, to accept that your XIB would contain a root view or something, which would be added as a subview onto the instance of your class put into Placement.

    This way, you should have something like:

    XIB with your custom view contents:

    XIB with your custom view contents

    Placement where you add your XIB:

    Placement where you add your XIB

    As the view instance added to your placement is the same as File Owner in your XIB you can set outlets and actions in both XIB and Placement. Just don’t forget you root view in your XIB is not the instance UIKit will create to place into Placement.

    For convenience purposes, please find my code below which is base class to ease the creation of such views:

    using System;
    
    using MonoTouch.ObjCRuntime;
    using MonoTouch.Foundation;
    using MonoTouch.UIKit;
    
    namespace Member.iOS.Base.Views {
    
        /// <summary>
        /// XibView is a Xib-backed UIView subclass which enables easy customization and custom
        /// behavior addition.
        /// </summary>
        public class XibView : UIView {
    
            /// <summary>
            /// Exception thrown when a loaded XIB doesn't contain any views inside it.
            /// </summary>
            class EmptyXibException : Exception {
            }
    
            /// <summary>
            /// Initializes a new instance of the <see cref="Member.iOS.Base.Views.XibView"/> class.
            /// </summary>
            /// <param name='handle'>
            /// Handle.
            /// </param>
            public XibView(IntPtr handle) : base(handle) {
            }
    
            /// <summary>
            /// Upon loading from a containing XIB, takes care of replacing the current instance (which acts as a stab) with
            /// a real view loaded from its XIB.
            /// </summary>
            public override void AwakeFromNib() {
                base.AwakeFromNib();
    
                NSArray views = NSBundle.MainBundle.LoadNib(GetType().Name, this, new NSDictionary());
    
                if (views.Count == 0) {
                    throw new EmptyXibException();
                }
    
                UIView rootView = Runtime.GetNSObject(views.ValueAt(0)) as UIView;
                rootView.Frame = new System.Drawing.RectangleF(0, 0, Frame.Width, Frame.Height);
                AddSubview(rootView);
            }
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I bind this using MonoTouch ?? #if __IPHONE_OS_VERSION_MAX_ALLOWED < 60000 @interface NSObject
I'm trying to create a custom UITableViewCell with a XIB using MonoTouch/MonoDevelop. When I
I am trying to implement a Settings dialog for iOS using MonoTouch and the
I am trying to integrate the TestFlightSdk into an app I've made using MonoTouch
I'm quite new at using Monotouch and I want to make an app that
Can we create an Accordion menu style for the iPhone using MonoTouch?
I'm using MonoTouch and my application setup looks something like this, NavController -TabBarController -NavController
I am trying to learn using MonoTouch. I am basically trying to create something
I am trying to build an app using MonoTouch and MonoTouch.Dialog. I really like
Using monotouch, I'm try to display a core text element over multiple lines. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.