Skip to content

Instantly share code, notes, and snippets.

@dgyesbreghs
Created December 27, 2018 12:45
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 dgyesbreghs/a49678562eb89bca6821a778aa2c6d77 to your computer and use it in GitHub Desktop.
Save dgyesbreghs/a49678562eb89bca6821a778aa2c6d77 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using CoreGraphics;
using UIKit;
namespace Kitta.Extensions
{
public enum KtDimension { Height, Width }
public static class UiViewExtensions
{
/// <summary>
/// Sets the view to a specific size.
/// </summary>
/// <param name="view"></param>
/// <param name="size"></param>
/// <returns></returns>
public static NSLayoutConstraint[] AutoSetDimension(this UIView view, CGSize size)
{
IList<NSLayoutConstraint> constraints = new List<NSLayoutConstraint>(2);
constraints.Add(view.AutoSetDimension(KtDimension.Height, size.Height));
constraints.Add(view.AutoSetDimension(KtDimension.Width, size.Width));
return constraints.ToArray();
}
/// <summary>
/// Sets the given dimension of the view to a specific size.
/// </summary>
/// <param name="view"></param>
/// <param name="dimension"></param>
/// <param name="constant"></param>
/// <returns></returns>
public static NSLayoutConstraint AutoSetDimension(this UIView view, KtDimension dimension, nfloat constant)
{
var constraint = new NSLayoutConstraint();
switch (dimension)
{
case KtDimension.Height:
constraint = view.HeightAnchor.ConstraintEqualTo(constant);
break;
case KtDimension.Width:
constraint = view.WidthAnchor.ConstraintEqualTo(constant);
break;
}
constraint.Active = true;
return constraint;
}
/// <summary>
/// Matches a dimension of the view to a given dimension of another view.
/// </summary>
/// <param name="view"></param>
/// <param name="dimension"></param>
/// <param name="peerView"></param>
/// <returns></returns>
public static NSLayoutConstraint AutoSetDimension(this UIView view, KtDimension dimension, UIView peerView)
{
return view.AutoSetDimension(dimension, peerView, 0);
}
/// <summary>
/// Matches a dimension of the view to a given dimension of another view with an offset.
/// </summary>
/// <param name="view"></param>
/// <param name="dimension"></param>
/// <param name="peerView"></param>
/// <param name="constant"></param>
/// <returns></returns>
public static NSLayoutConstraint AutoSetDimension(this UIView view, KtDimension dimension, UIView peerView,
nfloat constant)
{
var constraint = new NSLayoutConstraint();
switch (dimension)
{
case KtDimension.Height:
constraint = view.HeightAnchor.ConstraintEqualTo(peerView.HeightAnchor, constant);
break;
case KtDimension.Width:
constraint = view.WidthAnchor.ConstraintEqualTo(peerView.WidthAnchor, constant);
break;
}
constraint.Active = true;
return constraint;
}
/// <summary>
/// Return the layoutGuide for the correct iOS Version
/// Before iOS 11 -> Margin Area Guide
/// >= iOS 11 -> Safe Area Guide
/// </summary>
/// <param name="view"></param>
/// <returns></returns>
public static UILayoutGuide LayoutGuide(this UIView view)
{
return UIDevice.CurrentDevice.CheckSystemVersion(11, 0)
? view.SafeAreaLayoutGuide
: view.LayoutMarginsGuide;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment