Fixes for toast on iOS

This commit is contained in:
Kyle Spearrin 2017-12-22 16:59:36 -05:00
parent 839df123ff
commit e2fc5fff23

View file

@ -6,21 +6,17 @@ namespace Bit.iOS.Core.Views
{ {
public class Toast : UIView public class Toast : UIView
{ {
// Timer to dismiss the snack bar.
private NSTimer _dismissTimer; private NSTimer _dismissTimer;
// Constraints.
private NSLayoutConstraint _heightConstraint; private NSLayoutConstraint _heightConstraint;
private NSLayoutConstraint _leftMarginConstraint; private NSLayoutConstraint _leftMarginConstraint;
private NSLayoutConstraint _rightMarginConstraint; private NSLayoutConstraint _rightMarginConstraint;
private NSLayoutConstraint _topMarginConstraint;
private NSLayoutConstraint _bottomMarginConstraint; private NSLayoutConstraint _bottomMarginConstraint;
public Toast(string text) : base(CoreGraphics.CGRect.FromLTRB(0, 0, 320, 44)) public Toast(string text) : base(CoreGraphics.CGRect.FromLTRB(0, 0, 320, 44))
{ {
TranslatesAutoresizingMaskIntoConstraints = false; TranslatesAutoresizingMaskIntoConstraints = false;
BackgroundColor = UIColor.DarkGray; BackgroundColor = UIColor.DarkGray;
Layer.CornerRadius = 4; Layer.CornerRadius = 8;
Layer.MasksToBounds = true; Layer.MasksToBounds = true;
MessageLabel = new UILabel MessageLabel = new UILabel
@ -31,10 +27,13 @@ namespace Bit.iOS.Core.Views
BackgroundColor = UIColor.Clear, BackgroundColor = UIColor.Clear,
LineBreakMode = UILineBreakMode.WordWrap, LineBreakMode = UILineBreakMode.WordWrap,
TextAlignment = UITextAlignment.Center, TextAlignment = UITextAlignment.Center,
Lines = 0 Lines = 0,
Text = text
}; };
var hConstraints = NSLayoutConstraint.FromVisualFormat("H:|-5-[messageLabel]-5-|", 0, new NSDictionary(), AddSubview(MessageLabel);
var hMessageConstraints = NSLayoutConstraint.FromVisualFormat("H:|-5-[messageLabel]-5-|", 0, new NSDictionary(),
NSDictionary.FromObjectsAndKeys(new NSObject[] { MessageLabel }, NSDictionary.FromObjectsAndKeys(new NSObject[] { MessageLabel },
new NSObject[] { new NSString("messageLabel") }) new NSObject[] { new NSString("messageLabel") })
); );
@ -44,18 +43,18 @@ namespace Bit.iOS.Core.Views
new NSObject[] { new NSString("messageLabel") }) new NSObject[] { new NSString("messageLabel") })
); );
AddConstraints(hConstraints); AddConstraints(hMessageConstraints);
AddConstraints(vMessageConstraints); AddConstraints(vMessageConstraints);
AddSubview(MessageLabel);
AddGestureRecognizer(new UITapGestureRecognizer(() => Dismiss()));
} }
public TimeSpan Duration { get; set; } = TimeSpan.FromSeconds(3); public TimeSpan Duration { get; set; } = TimeSpan.FromSeconds(3);
public UILabel MessageLabel { get; set; } public UILabel MessageLabel { get; set; }
public nfloat LeftMargin { get; set; } = 4; public nfloat LeftMargin { get; set; } = 5;
public nfloat RightMargin { get; set; } = 4; public nfloat RightMargin { get; set; } = 5;
public nfloat BottomMargin { get; set; } = 4; public nfloat BottomMargin { get; set; } = 55;
public nfloat Height { get; set; } = 44; public nfloat Height { get; set; } = 44;
public nfloat TopMargin { get; set; } = 8;
public void Show() public void Show()
{ {
@ -94,7 +93,6 @@ namespace Bit.iOS.Core.Views
localSuperView.AddConstraint(_rightMarginConstraint); localSuperView.AddConstraint(_rightMarginConstraint);
localSuperView.AddConstraint(_bottomMarginConstraint); localSuperView.AddConstraint(_bottomMarginConstraint);
// Show
ShowWithAnimation(); ShowWithAnimation();
} }
else else
@ -126,10 +124,12 @@ namespace Bit.iOS.Core.Views
private void ShowWithAnimation() private void ShowWithAnimation()
{ {
Alpha = 0;
SetNeedsLayout();
_bottomMarginConstraint.Constant = -BottomMargin; _bottomMarginConstraint.Constant = -BottomMargin;
_leftMarginConstraint.Constant = LeftMargin; _leftMarginConstraint.Constant = LeftMargin;
_rightMarginConstraint.Constant = -RightMargin; _rightMarginConstraint.Constant = -RightMargin;
AnimateNotify(0.3f, 0, 0.7f, 5f, UIViewAnimationOptions.CurveEaseInOut, () => { Alpha = 0; }, null); AnimateNotify(0.3f, 0, 0.7f, 5f, UIViewAnimationOptions.CurveEaseInOut, () => { Alpha = 1; }, null);
} }
} }
} }