Xamarin.iOS Auto Layout in Xamarin.iOS Adding Constraints with Masonry

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

Masonry is a library for objective-c but xamarin have created a binding for it and created it as a nuget package https://www.nuget.org/packages/Masonry/.

Nuget install

Install-Package Masonry

This centers a button 100 points below the centre point of the containing view and sets a width between 200 and 400 points

this.loginBtn.MakeConstraints(make =>
{
    make.Width.GreaterThanOrEqualTo(new NSNumber(200));
    make.Width.LessThanOrEqualTo(new NSNumber(400));
    make.Center.EqualTo(this.View).CenterOffset(new CGPoint(0, 100));
});

This sets a scaled image 100 points above the centre point of the containing view then sets the width to the width of the containing view with a muliplier of 0.5 which means 50% of the width. It then sets the height to the width multiplied by the aspect ratio which causes the image to scale but maintain its correct aspect ratio

this.logo.MakeConstraints(make =>
{
    make.Center.EqualTo(this.View).CenterOffset(new CGPoint(0, -100));
    make.Width.EqualTo(this.View).MultipliedBy(0.5f);
    make.Height.EqualTo(this.logo.Width()).MultipliedBy(0.71f);
});


Got any Xamarin.iOS Question?