Skip to content

Instantly share code, notes, and snippets.

@omatrot
Created March 23, 2020 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omatrot/b6a92b67f9397d58dc8df8bc28de0a4b to your computer and use it in GitHub Desktop.
Save omatrot/b6a92b67f9397d58dc8df8bc28de0a4b to your computer and use it in GitHub Desktop.
Xamarin.iOS version of the code for this amazing tutorial about Making Programmatic Auto Layout Easy through Extensions
using System;
using CoreGraphics;
using UIKit;
namespace testlayout
{
public partial class ViewController : UIViewController
{
public ViewController(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.
var redView = new UIView();
redView.BackgroundColor = UIColor.Red;
var blueView = new UIView();
blueView.BackgroundColor = UIColor.Blue;
var greenView = new UIView();
greenView.BackgroundColor = UIColor.Green;
UIView[] viewArray = new UIView[] { redView, blueView, greenView };
View.AddSubviews(viewArray);
//// enable auto layout
//redView.TranslatesAutoresizingMaskIntoConstraints = false;
//redView.TopAnchor.ConstraintEqualTo(View.TopAnchor).Active = true;
//redView.LeadingAnchor.ConstraintEqualTo(View.LeadingAnchor).Active = true;
//redView.TrailingAnchor.ConstraintEqualTo(View.TrailingAnchor).Active = true;
//redView.BottomAnchor.ConstraintEqualTo(View.BottomAnchor).Active = true;
//redView.Anchor(top: View.SafeAreaLayoutGuide.TopAnchor, leading: View.LeadingAnchor, bottom: View.SafeAreaLayoutGuide.BottomAnchor, trailing: View.TrailingAnchor, padding: new UIEdgeInsets(0, 16, 0, 16), size: new CGSize(100, 100));
redView.Anchor(top: View.SafeAreaLayoutGuide.TopAnchor, trailing: View.TrailingAnchor, padding: new UIEdgeInsets(0, 16, 0, 16), size: new CGSize(125, 0));
redView.HeightAnchor.ConstraintEqualTo(redView.WidthAnchor).Active = true;
blueView.Anchor(top: redView.BottomAnchor, trailing: redView.TrailingAnchor, padding: new UIEdgeInsets(12, 0, 0, 0));
blueView.AnchorSize(to: redView);
//blueView.WidthAnchor.ConstraintEqualTo(redView.WidthAnchor).Active = true;
//blueView.HeightAnchor.ConstraintEqualTo(blueView.WidthAnchor).Active = true;
greenView.Anchor(top: redView.TopAnchor, leading: View.SafeAreaLayoutGuide.LeadingAnchor, bottom: blueView.BottomAnchor, trailing: redView.LeadingAnchor, padding: new UIEdgeInsets(0, 12, 0, 12));
var purpleView = new UIView();
purpleView.BackgroundColor = UIColor.Purple;
View.AddSubview(purpleView);
purpleView.FillParentView();
}
public override void DidReceiveMemoryWarning()
{
base.DidReceiveMemoryWarning();
// Release any cached data, images, etc that aren't in use.
}
}
internal static class extensions
{
internal static void FillParentView(this UIView uIView)
{
uIView.Anchor(top: uIView.Superview?.TopAnchor, leading: uIView.Superview?.LeadingAnchor, bottom: uIView.Superview?.BottomAnchor, trailing: uIView.Superview.TrailingAnchor);
}
internal static void AnchorSize(this UIView uIView, UIView to)
{
uIView.WidthAnchor.ConstraintEqualTo(to.WidthAnchor).Active = true;
uIView.HeightAnchor.ConstraintEqualTo(to.HeightAnchor).Active = true;
}
internal static void Anchor(this UIView uIView, NSLayoutYAxisAnchor top = null, NSLayoutXAxisAnchor leading = null, NSLayoutYAxisAnchor bottom = null, NSLayoutXAxisAnchor trailing = null, UIEdgeInsets padding = default, CGSize size= default)
{
uIView.TranslatesAutoresizingMaskIntoConstraints = false;
if (top != null)
{
uIView.TopAnchor.ConstraintEqualTo(top, padding.Top).Active = true;
}
if (leading != null)
{
uIView.LeadingAnchor.ConstraintEqualTo(leading, padding.Left).Active = true;
}
if (bottom != null)
{
uIView.BottomAnchor.ConstraintEqualTo(bottom, -padding.Bottom).Active = true;
}
if (trailing != null)
{
uIView.TrailingAnchor.ConstraintEqualTo(trailing, -padding.Right).Active = true;
}
if ( size.Width != 0)
{
uIView.WidthAnchor.ConstraintEqualTo(size.Width).Active = true;
}
if ( size.Height != 0)
{
uIView.HeightAnchor.ConstraintEqualTo(size.Height).Active = true;
}
}
}
}
@omatrot
Copy link
Author

omatrot commented Mar 23, 2020

The YouTube video is here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment