UIView.AddConstraints(NSLayoutConstraint[]) UIView.AddConstraints(NSLayoutConstraint[]) Method


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

[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



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


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.


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