Edit

Share via


UIButton Constructors

Definition

Overloads

UIButton()

Default constructor that initializes a new instance of this class with no parameters.

UIButton(CGRect)

Initializes the UIButton with the specified frame.

UIButton(NSCoder)

A constructor that initializes the object from the data stored in the unarchiver object.

UIButton(NSObjectFlag)

Constructor to call on derived classes to skip initialization and merely allocate the object.

UIButton(IntPtr)

A constructor used when creating managed representations of unmanaged objects; Called by the runtime.

UIButton(UIButtonType)

Preferred constructor for UIButtons, but can not be used when subclassing.

UIButton()

Default constructor that initializes a new instance of this class with no parameters.

[Foundation.Export("init")]
public UIButton ();
Attributes

Applies to

UIButton(CGRect)

Initializes the UIButton with the specified frame.

[Foundation.Export("initWithFrame:")]
public UIButton (CoreGraphics.CGRect frame);
new UIKit.UIButton : CoreGraphics.CGRect -> UIKit.UIButton

Parameters

frame
CGRect

Frame used by the view, expressed in iOS points.

Attributes

Remarks

This constructor is used to programmatically create a new instance of UIButton with the specified dimension in the frame. The object will only be displayed once it has been added to a view hierarchy by calling AddSubview in a containing view.

This constructor is not invoked when deserializing objects from storyboards or XIB filesinstead the constructor that takes an NSCoder parameter is invoked.

Applies to

UIButton(NSCoder)

A constructor that initializes the object from the data stored in the unarchiver object.

[Foundation.Export("initWithCoder:")]
[ObjCRuntime.DesignatedInitializer]
public UIButton (Foundation.NSCoder coder);
new UIKit.UIButton : Foundation.NSCoder -> UIKit.UIButton

Parameters

coder
NSCoder

The unarchiver object.

Attributes

Remarks

This constructor is provided to allow the class to be initialized from an unarchiver (for example, during NIB deserialization). This is part of the NSCoding protocol.

If developers want to create a subclass of this object and continue to support deserialization from an archive, they should implement a constructor with an identical signature: taking a single parameter of type NSCoder and decorate it with the [Export("initWithCoder:"] attribute declaration.

The state of this object can also be serialized by using the companion method, EncodeTo.

Applies to

UIButton(NSObjectFlag)

Constructor to call on derived classes to skip initialization and merely allocate the object.

protected UIButton (Foundation.NSObjectFlag t);
new UIKit.UIButton : Foundation.NSObjectFlag -> UIKit.UIButton

Parameters

t
NSObjectFlag

Unused sentinel value, pass NSObjectFlag.Empty.

Remarks

This constructor should be called by derived classes when they completely construct the object in managed code and merely want the runtime to allocate and initialize the NSObject. This is required to implement the two-step initialization process that Objective-C uses, the first step is to perform the object allocation, the second step is to initialize the object. When developers invoke the constructor that takes the NSObjectFlag.Empty they take advantage of a direct path that goes all the way up to NSObject to merely allocate the object's memory and bind the Objective-C and C# objects together. The actual initialization of the object is up to the developer.

This constructor is typically used by the binding generator to allocate the object, but prevent the actual initialization to take place. Once the allocation has taken place, the constructor has to initialize the object. With constructors generated by the binding generator this means that it manually invokes one of the "init" methods to initialize the object.

It is the developer's responsibility to completely initialize the object if they chain up using the NSObjectFlag.Empty path.

In general, if the developer's constructor invokes the NSObjectFlag.Empty base implementation, then it should be calling an Objective-C init method. If this is not the case, developers should instead chain to the proper constructor in their class.

The argument value is ignored and merely ensures that the only code that is executed is the construction phase is the basic NSObject allocation and runtime type registration. Typically the chaining would look like this:

//
// The NSObjectFlag merely allocates the object and registers the
// C# class with the Objective-C runtime if necessary, but no actual
// initXxx method is invoked, that is done later in the constructor
//
// This is taken from Xamarin.iOS's source code:
//
[Export ("initWithFrame:")]
public UIView (System.Drawing.RectangleF frame) : base (NSObjectFlag.Empty)
{
// Invoke the init method now.
	var initWithFrame = new Selector ("initWithFrame:").Handle;
	if (IsDirectBinding)
		Handle = ObjCRuntime.Messaging.IntPtr_objc_msgSend_CGRect (this.Handle, initWithFrame, frame);
	else
		Handle = ObjCRuntime.Messaging.IntPtr_objc_msgSendSuper_CGRect (this.SuperHandle, initWithFrame, frame);
}

Applies to

UIButton(IntPtr)

A constructor used when creating managed representations of unmanaged objects; Called by the runtime.

protected internal UIButton (IntPtr handle);
new UIKit.UIButton : nativeint -> UIKit.UIButton

Parameters

handle
IntPtr

nativeint

Pointer (handle) to the unmanaged object.

Remarks

This constructor is invoked by the runtime infrastructure (GetNSObject(IntPtr)) to create a new managed representation for a pointer to an unmanaged Objective-C object. Developers should not invoke this method directly, instead they should call the GetNSObject method as it will prevent two instances of a managed object to point to the same native object.

Applies to

UIButton(UIButtonType)

Preferred constructor for UIButtons, but can not be used when subclassing.

public UIButton (UIKit.UIButtonType type);
new UIKit.UIButton : UIKit.UIButtonType -> UIKit.UIButton

Parameters

type
UIButtonType

Button type to create.

Remarks

This constructor should only be used in user code when you plan on using the C# syntax to initialize properties after the object has been created, like this:

var b = new UIButton (UIButtonType.Custom) {
    AdjustsImageWhenHighlighted = true,
    ContentEdgeInsets = myInsets;
};

This constructor can not be used when subclassing as it is not a real constructor, but instead a convenience constructor that calls the static FromType(UIButtonType) method and then wraps the result into this object. So you should avoid subclassing UIButton and then calling into this constructor from your subclass constructor.

Applies to