private void Initialize() { lunchDuration = NBLunchDuration.Medium; hasRoundedCorners = true; UserInteractionEnabled = false; BackgroundColor = kDefaultBackground; }
/** * Displays a snackbar (new in material design) at the bottom of the screen * - parameter text: The message to be displayed * - parameter duration: The duration of the snackbar */ public static void Show(string text, NBLunchDuration duration, UIView windowView = null) { if (windowView == null) { windowView = UIApplication.SharedApplication.GetTopView(); } NBMaterialSnackbar toast = NBMaterialSnackbar.CreateSingleWithTextAndDuration(windowView, text: text, duration: duration); toast.Show(); }
/** * Displays a classic toast message with a user defined text and duration * - parameter windowView: The window which the toast is to be attached * - parameter text: The message to be displayed * - parameter duration: The duration of the toast */ public static void Show(string text, NBLunchDuration duration, ToastStyle style, UIView windowView = null) { if (style == null) { throw new ArgumentNullException(nameof(style)); } if (windowView == null) { windowView = UIApplication.SharedApplication.GetTopView(); } var toast = CreateWithTextAndConstraints(windowView, text, duration, style); toast.Show(); }
private static NBMaterialSnackbar CreateSingleWithTextAndDuration(UIView windowView, string text, NBLunchDuration duration) { NBMaterialSnackbar snack = new NBMaterialSnackbar(); snack.lunchDuration = duration; snack.TranslatesAutoresizingMaskIntoConstraints = false; snack.currentHeight = snack.kMinHeight; snack.textLabel = new UILabel(); snack.textLabel.BackgroundColor = UIColor.Clear; snack.textLabel.TextAlignment = UITextAlignment.Left; snack.textLabel.Font = snack.kFontRoboto; snack.textLabel.TextColor = snack.kFontColor; snack.textLabel.Lines = 1; snack.textLabel.Alpha = 0.0f; snack.textLabel.TranslatesAutoresizingMaskIntoConstraints = false; snack.textLabel.Text = text; snack.AddSubview(snack.textLabel); windowView.AddSubview(snack); snack.constraintViews.SetValueForKey(snack.textLabel, new NSString("textLabel")); snack.constraintViews.SetValueForKey(snack, new NSString("snack")); snack.constraintMetrics.SetValueForKey(new NSNumber(snack.kVerticalSinglePadding), new NSString("vPad")); snack.constraintMetrics.SetValueForKey(new NSNumber(snack.kHorizontalPadding), new NSString("hPad")); snack.constraintMetrics.SetValueForKey(new NSNumber(snack.kMinHeight), new NSString("minHeight")); snack.AddConstraints(NSLayoutConstraint.FromVisualFormat("V:|-vPad-[textLabel]-vPad-|", NSLayoutFormatOptions.DirectionLeadingToTrailing, metrics: snack.constraintMetrics, views: snack.constraintViews)); snack.AddConstraints(NSLayoutConstraint.FromVisualFormat("H:|-hPad-[textLabel]-hPad-|", NSLayoutFormatOptions.DirectionLeadingToTrailing, metrics: snack.constraintMetrics, views: snack.constraintViews)); windowView.AddConstraints(NSLayoutConstraint.FromVisualFormat("V:[snack(==minHeight)]", NSLayoutFormatOptions.DirectionLeadingToTrailing, metrics: snack.constraintMetrics, views: snack.constraintViews)); windowView.AddConstraints(NSLayoutConstraint.FromVisualFormat("H:|[snack]|", NSLayoutFormatOptions.DirectionLeadingToTrailing, metrics: snack.constraintMetrics, views: snack.constraintViews)); snack.verticalConstraint = NSLayoutConstraint.Create((NSObject)snack, (NSLayoutAttribute)NSLayoutAttribute.Bottom, (NSLayoutRelation)NSLayoutRelation.Equal, (NSObject)windowView, (NSLayoutAttribute)NSLayoutAttribute.Bottom, (nfloat)1.0, constant: snack.currentHeight); windowView.AddConstraint(snack.verticalConstraint); return(snack); }
private static NBMaterialToast CreateWithTextAndConstraints(UIView windowView, string text, NBLunchDuration duration, ToastStyle style) { if (style == null) { throw new ArgumentNullException(nameof(style)); } var toast = new NBMaterialToast { lunchDuration = duration, Alpha = 0.0f, TranslatesAutoresizingMaskIntoConstraints = false, BackgroundColor = style.BackgroundColor }; var textLabel = new UILabel { BackgroundColor = UIColor.Clear, TextAlignment = UITextAlignment.Left, Font = toast.kFontRoboto, TextColor = style.FontColor, Lines = 0, TranslatesAutoresizingMaskIntoConstraints = false, AccessibilityIdentifier = "NbMaterialToastLabel", Text = text }; if (toast.hasRoundedCorners) { toast.Layer.MasksToBounds = true; toast.Layer.CornerRadius = 15.0f; } toast.AddSubview(textLabel); windowView.AddSubview(toast); toast.constraintViews.SetValueForKey(textLabel, new NSString("textLabel")); toast.constraintViews.SetValueForKey(toast, new NSString("toast")); var verticalBottomMargin = toast.kVerticalBottomMargin; if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0)) { var insets = UIApplication.SharedApplication.Windows[0].SafeAreaInsets; if (insets.Bottom > 0) { verticalBottomMargin += insets.Bottom; } } toast.constraintMetrics.SetValueForKey(new NSNumber(toast.kVerticalPadding), new NSString("vPad")); toast.constraintMetrics.SetValueForKey(new NSNumber(toast.kHorizontalPadding), new NSString("hPad")); toast.constraintMetrics.SetValueForKey(new NSNumber(toast.kMinHeight), new NSString("minHeight")); toast.constraintMetrics.SetValueForKey(new NSNumber(verticalBottomMargin), new NSString("vMargin")); toast.constraintMetrics.SetValueForKey(new NSNumber(toast.kHorizontalMargin), new NSString("hMargin")); toast.AddConstraints(NSLayoutConstraint.FromVisualFormat("V:|-vPad-[textLabel]-vPad-|", NSLayoutFormatOptions.DirectionLeadingToTrailing, metrics: toast.constraintMetrics, views: toast.constraintViews)); toast.AddConstraints(NSLayoutConstraint.FromVisualFormat("H:|-hPad-[textLabel]-hPad-|", NSLayoutFormatOptions.DirectionLeadingToTrailing, metrics: toast.constraintMetrics, views: toast.constraintViews)); toast.SetContentHuggingPriority(750, UILayoutConstraintAxis.Vertical); toast.SetContentHuggingPriority(750, UILayoutConstraintAxis.Horizontal); toast.SetContentCompressionResistancePriority(750, UILayoutConstraintAxis.Horizontal); windowView.AddConstraints(NSLayoutConstraint.FromVisualFormat("V:[toast(>=minHeight)]-vMargin-|", NSLayoutFormatOptions.DirectionLeadingToTrailing, metrics: toast.constraintMetrics, views: toast.constraintViews)); windowView.AddConstraints(NSLayoutConstraint.FromVisualFormat("H:|-(>=hMargin)-[toast]-(>=hMargin)-|", NSLayoutFormatOptions.DirectionLeadingToTrailing, metrics: toast.constraintMetrics, views: toast.constraintViews)); windowView.AddConstraint(NSLayoutConstraint.Create(toast, NSLayoutAttribute.CenterX, NSLayoutRelation.Equal, windowView, NSLayoutAttribute.CenterX, multiplier: 1.0f, constant: 0)); return(toast); }