Skip to content

Commit

Permalink
Merge pull request #471 from ThomasDhaen/master
Browse files Browse the repository at this point in the history
iOS Safe Area implementation
  • Loading branch information
aritchie authored Jan 12, 2018
2 parents de7eebd + 07acd63 commit e950dfe
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 8 deletions.
20 changes: 18 additions & 2 deletions src/Acr.UserDialogs.iOS/AIDatePickerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace AI
[Register ("AIDatePickerController")]
public class AIDatePickerController : UIViewController, IUIViewControllerAnimatedTransitioning, IUIViewControllerTransitioningDelegate
{
private UIEdgeInsets safeAreaInsets;

public double AnimatedTransitionDuration { get; set; } = 0.4;
public UIDatePickerMode Mode { get; set; } = UIDatePickerMode.Date;
public UIColor BackgroundColor { get; set; } = UIColor.White;
Expand All @@ -31,7 +33,9 @@ public AIDatePickerController()
{
//this.ModalPresentationStyle = UIModalPresentationStyle.Custom;
this.ModalPresentationStyle = UIModalPresentationStyle.OverCurrentContext;
this.TransitioningDelegate = this;
this.TransitioningDelegate = this;

SetupSafeAreaInsets();
}


Expand Down Expand Up @@ -165,9 +169,21 @@ public override void ViewDidLoad()
this.View.AddConstraints(NSLayoutConstraint.FromVisualFormat("H:|-5-[DatePickerContainerView]-5-|", 0, null, views));
this.View.AddConstraints(NSLayoutConstraint.FromVisualFormat("H:|-5-[ButtonContainerView]-5-|", 0, null, views));

this.View.AddConstraints(NSLayoutConstraint.FromVisualFormat("V:|[DismissButton][DatePickerContainerView]-10-[ButtonContainerView(40)]-5-|", 0, null, views));
this.View.AddConstraints(NSLayoutConstraint.FromVisualFormat($"V:|[DismissButton][DatePickerContainerView]-10-[ButtonContainerView(40)]-{5 + safeAreaInsets.Bottom}-|", 0, null, views));
}

private void SetupSafeAreaInsets()
{
if(UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
{
safeAreaInsets = UIApplication.SharedApplication.KeyWindow.SafeAreaInsets;
}
else
{
safeAreaInsets = new UIEdgeInsets();
}
}

public double TransitionDuration(IUIViewControllerContextTransitioning transitionContext)
{
return AnimatedTransitionDuration;
Expand Down
69 changes: 63 additions & 6 deletions src/Acr.UserDialogs.iOS/TTGSnackbar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ public class TTGSnackbar : UIView
// Snackbar icon imageView default width
private const float snackbarIconImageViewWidth = 32;

private UIEdgeInsets safeAreaInsets;

public Action<TTGSnackbar> ActionBlock { get; set; }
public Action<TTGSnackbar> SecondActionBlock { get; set; }


public nfloat TopMargin { get; set; } = 8;
/// <summary>
/// Snackbar display duration. Default is 3 seconds.
/// </summary>
Expand Down Expand Up @@ -61,12 +61,55 @@ public nfloat CornerRadius
}
}

public nfloat LeftMargin { get; set; } = 4;
public nfloat RightMargin { get; set; } = 4;
nfloat topMargin = 8;
public nfloat TopMargin
{
get
{
return topMargin + safeAreaInsets.Top;
}
set
{
topMargin = value;
}
}

nfloat leftMargin = 4;
public nfloat LeftMargin
{
get
{
return leftMargin + safeAreaInsets.Left;
}
set
{
leftMargin = value;
}
}

nfloat rightMargin = 4;
public nfloat RightMargin
{
get
{
return rightMargin + safeAreaInsets.Right;
}
set
{
rightMargin = value;
}
}

/// Bottom margin. Default is 4
public nfloat BottomMargin { get; set; } = 4;
nfloat bottomMargin = 4;
public nfloat BottomMargin {
get {
return bottomMargin + safeAreaInsets.Bottom;
}
set {
bottomMargin = value;
}
}
public nfloat Height { get; set; } = 44;


Expand Down Expand Up @@ -152,6 +195,8 @@ public TTGSnackbar() : base(CoreGraphics.CGRect.FromLTRB(0, 0, 320, 44))
this.Layer.CornerRadius = 4;
this.Layer.MasksToBounds = true;

SetupSafeAreaInsets();

this.MessageLabel = new UILabel
{
TranslatesAutoresizingMaskIntoConstraints = false,
Expand Down Expand Up @@ -294,6 +339,18 @@ public TTGSnackbar() : base(CoreGraphics.CGRect.FromLTRB(0, 0, 320, 44))
//this.AddConstraints(hConstraintsForActivityIndicatorView);
}

private void SetupSafeAreaInsets()
{
if(UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
{
safeAreaInsets = UIApplication.SharedApplication.KeyWindow.SafeAreaInsets;
}
else
{
safeAreaInsets = new UIEdgeInsets();
}
}

/// <summary>
/// Show the snackbar
/// </summary>
Expand Down Expand Up @@ -364,7 +421,7 @@ public void Show()
localSuperView,
NSLayoutAttribute.Bottom,
1,
-BottomMargin);
-BottomMargin );

// Avoid the "UIView-Encapsulated-Layout-Height" constraint conflicts
// http://stackoverflow.com/questions/25059443/what-is-nslayoutconstraint-uiview-encapsulated-layout-height-and-how-should-i
Expand Down

0 comments on commit e950dfe

Please sign in to comment.