UIView.AddConstraints(NSLayoutConstraint[]) Method

Definition

Adds multiple constraints to the layout of the receiving view or its subviews.

[Foundation.Export("addConstraints:")]
[ObjCRuntime.Introduced(ObjCRuntime.PlatformName.iOS, 6, 0, ObjCRuntime.PlatformArchitecture.All, null)]
public virtual void AddConstraints (UIKit.NSLayoutConstraint[] constraints);
abstract member AddConstraints : UIKit.NSLayoutConstraint[] -> unit
override this.AddConstraints : UIKit.NSLayoutConstraint[] -> unit

Parameters

constraints
NSLayoutConstraint[]

An array of NSLayoutConstraints to add. The constraints must refer only to the receiving view or its subviews.

Attributes

Remarks

The constraints must refer only to the receiving view or its subviews.

In the following example, Visual Format Language specifies that the blueView be horizontally attched to the leading and trailing edge (sides), that the blueView and greenView be vertically attached to the leading and trailing edges (top and bottom) and separated by 30 pixels, and that the greenView be the same width as the blueView and attached to both the leading and trailing edges. The result is shown in the following image.

mainView.AddSubview(blueView);
mainView.AddSubview(greenView);

greenView.TranslatesAutoresizingMaskIntoConstraints = false;
blueView.TranslatesAutoresizingMaskIntoConstraints = false;

var viewsDictionary = NSDictionary.FromObjectsAndKeys(new NSObject[] { greenView, blueView}, new NSObject[] { new NSString("green"), new NSString("blue")});
var metrics = new NSDictionary();

mainView.AddConstraints(NSLayoutConstraint.FromVisualFormat("H:|-[blue]-|",0, new NSDictionary(), viewsDictionary));
mainView.AddConstraints(NSLayoutConstraint.FromVisualFormat("V:|-[blue]-(==30)-[green(==blue)]-|",0, new NSDictionary(), viewsDictionary));
mainView.AddConstraints(NSLayoutConstraint.FromVisualFormat("|-[green(==blue)]-|",0, new NSDictionary(), viewsDictionary));

Applies to

See also