Skip to content

Commit e950dfe

Browse files
authored
Merge pull request #471 from ThomasDhaen/master
iOS Safe Area implementation
2 parents de7eebd + 07acd63 commit e950dfe

File tree

2 files changed

+81
-8
lines changed

2 files changed

+81
-8
lines changed

src/Acr.UserDialogs.iOS/AIDatePickerController.cs

+18-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ namespace AI
88
[Register ("AIDatePickerController")]
99
public class AIDatePickerController : UIViewController, IUIViewControllerAnimatedTransitioning, IUIViewControllerTransitioningDelegate
1010
{
11+
private UIEdgeInsets safeAreaInsets;
12+
1113
public double AnimatedTransitionDuration { get; set; } = 0.4;
1214
public UIDatePickerMode Mode { get; set; } = UIDatePickerMode.Date;
1315
public UIColor BackgroundColor { get; set; } = UIColor.White;
@@ -31,7 +33,9 @@ public AIDatePickerController()
3133
{
3234
//this.ModalPresentationStyle = UIModalPresentationStyle.Custom;
3335
this.ModalPresentationStyle = UIModalPresentationStyle.OverCurrentContext;
34-
this.TransitioningDelegate = this;
36+
this.TransitioningDelegate = this;
37+
38+
SetupSafeAreaInsets();
3539
}
3640

3741

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

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

175+
private void SetupSafeAreaInsets()
176+
{
177+
if(UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
178+
{
179+
safeAreaInsets = UIApplication.SharedApplication.KeyWindow.SafeAreaInsets;
180+
}
181+
else
182+
{
183+
safeAreaInsets = new UIEdgeInsets();
184+
}
185+
}
186+
171187
public double TransitionDuration(IUIViewControllerContextTransitioning transitionContext)
172188
{
173189
return AnimatedTransitionDuration;

src/Acr.UserDialogs.iOS/TTGSnackbar.cs

+63-6
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ public class TTGSnackbar : UIView
2828
// Snackbar icon imageView default width
2929
private const float snackbarIconImageViewWidth = 32;
3030

31+
private UIEdgeInsets safeAreaInsets;
32+
3133
public Action<TTGSnackbar> ActionBlock { get; set; }
3234
public Action<TTGSnackbar> SecondActionBlock { get; set; }
3335

34-
35-
public nfloat TopMargin { get; set; } = 8;
3636
/// <summary>
3737
/// Snackbar display duration. Default is 3 seconds.
3838
/// </summary>
@@ -61,12 +61,55 @@ public nfloat CornerRadius
6161
}
6262
}
6363

64-
public nfloat LeftMargin { get; set; } = 4;
65-
public nfloat RightMargin { get; set; } = 4;
64+
nfloat topMargin = 8;
65+
public nfloat TopMargin
66+
{
67+
get
68+
{
69+
return topMargin + safeAreaInsets.Top;
70+
}
71+
set
72+
{
73+
topMargin = value;
74+
}
75+
}
6676

77+
nfloat leftMargin = 4;
78+
public nfloat LeftMargin
79+
{
80+
get
81+
{
82+
return leftMargin + safeAreaInsets.Left;
83+
}
84+
set
85+
{
86+
leftMargin = value;
87+
}
88+
}
89+
90+
nfloat rightMargin = 4;
91+
public nfloat RightMargin
92+
{
93+
get
94+
{
95+
return rightMargin + safeAreaInsets.Right;
96+
}
97+
set
98+
{
99+
rightMargin = value;
100+
}
101+
}
67102

68103
/// Bottom margin. Default is 4
69-
public nfloat BottomMargin { get; set; } = 4;
104+
nfloat bottomMargin = 4;
105+
public nfloat BottomMargin {
106+
get {
107+
return bottomMargin + safeAreaInsets.Bottom;
108+
}
109+
set {
110+
bottomMargin = value;
111+
}
112+
}
70113
public nfloat Height { get; set; } = 44;
71114

72115

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

198+
SetupSafeAreaInsets();
199+
155200
this.MessageLabel = new UILabel
156201
{
157202
TranslatesAutoresizingMaskIntoConstraints = false,
@@ -294,6 +339,18 @@ public TTGSnackbar() : base(CoreGraphics.CGRect.FromLTRB(0, 0, 320, 44))
294339
//this.AddConstraints(hConstraintsForActivityIndicatorView);
295340
}
296341

342+
private void SetupSafeAreaInsets()
343+
{
344+
if(UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
345+
{
346+
safeAreaInsets = UIApplication.SharedApplication.KeyWindow.SafeAreaInsets;
347+
}
348+
else
349+
{
350+
safeAreaInsets = new UIEdgeInsets();
351+
}
352+
}
353+
297354
/// <summary>
298355
/// Show the snackbar
299356
/// </summary>
@@ -364,7 +421,7 @@ public void Show()
364421
localSuperView,
365422
NSLayoutAttribute.Bottom,
366423
1,
367-
-BottomMargin);
424+
-BottomMargin );
368425

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

0 commit comments

Comments
 (0)