Exemplo n.º 1
0
        public TTGSnackbar() : base(CoreGraphics.CGRect.FromLTRB(0, 0, 320, 44))
        {
            this.TranslatesAutoresizingMaskIntoConstraints = false;
            this.BackgroundColor     = UIColor.DarkGray;
            this.Layer.CornerRadius  = 4;
            this.Layer.MasksToBounds = true;

            SetupSafeAreaInsets();

            this.MessageLabel = new UILabel
            {
                TranslatesAutoresizingMaskIntoConstraints = false,
                TextColor       = UIColor.White,
                Font            = UIFont.SystemFontOfSize(14),
                BackgroundColor = UIColor.Clear,
                LineBreakMode   = UILineBreakMode.WordWrap,
                TextAlignment   = UITextAlignment.Left,
                Lines           = 0
            };

            this.AddSubview(this.MessageLabel);

            IconImageView = new UIImageView()
            {
                TranslatesAutoresizingMaskIntoConstraints = false,
                BackgroundColor = UIColor.Clear,
                ContentMode     = IconContentMode
            };

            this.AddSubview(IconImageView);

            this.ActionButton = new UIButton
            {
                TranslatesAutoresizingMaskIntoConstraints = false,
                BackgroundColor = UIColor.Clear
            };
            this.ActionButton.TitleLabel.Font = UIFont.SystemFontOfSize(14);
            this.ActionButton.TitleLabel.AdjustsFontSizeToFitWidth = true;
            this.ActionButton.SetTitleColor(UIColor.White, UIControlState.Normal);
            this.ActionButton.TouchUpInside += (s, e) =>
            {
                // there is a chance that user doesn't want to do anything here, he simply wants to dismiss
                ActionBlock?.Invoke(this);
                Dismiss();
            };

            this.AddSubview(this.ActionButton);

            this.SecondActionButton = new UIButton
            {
                TranslatesAutoresizingMaskIntoConstraints = false,
                BackgroundColor = UIColor.Clear
            };
            this.SecondActionButton.TitleLabel.Font = UIFont.BoldSystemFontOfSize(14);
            this.SecondActionButton.TitleLabel.AdjustsFontSizeToFitWidth = true;
            this.SecondActionButton.SetTitleColor(UIColor.White, UIControlState.Normal);
            this.SecondActionButton.TouchUpInside += (s, e) =>
            {
                SecondActionBlock?.Invoke(this);
                Dismiss();
            };

            this.AddSubview(this.SecondActionButton);

            this.seperateView = new UIView
            {
                TranslatesAutoresizingMaskIntoConstraints = false,
                BackgroundColor = UIColor.Gray
            };

            this.AddSubview(this.seperateView);

            // Add constraints
            var hConstraints = NSLayoutConstraint.FromVisualFormat(
                "H:|-0-[iconImageView]-2-[messageLabel]-2-[seperateView(0.5)]-2-[actionButton(>=44@999)]-0-[secondActionButton(>=44@999)]-0-|",
                0,
                new NSDictionary(),
                NSDictionary.FromObjectsAndKeys(
                    new NSObject[] {
                IconImageView,
                MessageLabel,
                seperateView,
                ActionButton,
                SecondActionButton
            },
                    new NSObject[] {
                new NSString("iconImageView"),
                new NSString("messageLabel"),
                new NSString("seperateView"),
                new NSString("actionButton"),
                new NSString("secondActionButton")
            }
                    )
                );

            var vConstraintsForIconImageView = NSLayoutConstraint.FromVisualFormat(
                "V:|-2-[iconImageView]-2-|", 0, new NSDictionary(), NSDictionary.FromObjectsAndKeys(new NSObject[] { IconImageView }, new NSObject[] { new NSString("iconImageView") })
                );

            var vConstraintsForMessageLabel = NSLayoutConstraint.FromVisualFormat(
                "V:|-0-[messageLabel]-0-|", 0, new NSDictionary(), NSDictionary.FromObjectsAndKeys(new NSObject[] { MessageLabel }, new NSObject[] { new NSString("messageLabel") })
                );

            var vConstraintsForSeperateView = NSLayoutConstraint.FromVisualFormat(
                "V:|-4-[seperateView]-4-|", 0, new NSDictionary(), NSDictionary.FromObjectsAndKeys(new NSObject[] { seperateView }, new NSObject[] { new NSString("seperateView") })
                );

            var vConstraintsForActionButton = NSLayoutConstraint.FromVisualFormat(
                "V:|-0-[actionButton]-0-|", 0, new NSDictionary(), NSDictionary.FromObjectsAndKeys(new NSObject[] { ActionButton }, new NSObject[] { new NSString("actionButton") })
                );

            var vConstraintsForSecondActionButton = NSLayoutConstraint.FromVisualFormat(
                "V:|-0-[secondActionButton]-0-|", 0, new NSDictionary(), NSDictionary.FromObjectsAndKeys(new NSObject[] { SecondActionButton }, new NSObject[] { new NSString("secondActionButton") })
                );

            iconImageViewWidthConstraint = NSLayoutConstraint.Create(IconImageView, NSLayoutAttribute.Width, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1, TTGSnackbar.snackbarIconImageViewWidth);

            actionButtonWidthConstraint = NSLayoutConstraint.Create(ActionButton, NSLayoutAttribute.Width, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1, TTGSnackbar.snackbarActionButtonMinWidth);

            secondActionButtonWidthConstraint = NSLayoutConstraint.Create(SecondActionButton, NSLayoutAttribute.Width, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1, TTGSnackbar.snackbarActionButtonMinWidth);

            //var vConstraintsForActivityIndicatorView = NSLayoutConstraint.FromVisualFormat(
            //"V:|-2-[activityIndicatorView]-2-|", 0,new NSDictionary(), NSDictionary.FromObjectsAndKeys(new NSObject[] { activityIndicatorView }, new NSObject[] { new NSString("activityIndicatorView") })
            //);

            //todo fix constraint
            //var hConstraintsForActivityIndicatorView = NSLayoutConstraint.FromVisualFormat(
            //  //"H:[activityIndicatorView(activityIndicatorWidth)]-2-|",
            //  "H:[activityIndicatorView]-2-|",
            //  0,
            //  new NSDictionary(),
            //  NSDictionary.FromObjectsAndKeys(
            //      new NSObject[] {  activityIndicatorView },
            //                 new NSObject[] {  new NSString("activityIndicatorView") })
            //  //NSDictionary.FromObjectsAndKeys(new NSObject[] { activityIndicatorView }, new NSObject[] {  })
            //);

            IconImageView.AddConstraint(iconImageViewWidthConstraint);
            ActionButton.AddConstraint(actionButtonWidthConstraint);
            SecondActionButton.AddConstraint(secondActionButtonWidthConstraint);

            this.AddConstraints(hConstraints);
            this.AddConstraints(vConstraintsForIconImageView);
            this.AddConstraints(vConstraintsForMessageLabel);
            this.AddConstraints(vConstraintsForSeperateView);
            this.AddConstraints(vConstraintsForActionButton);
            this.AddConstraints(vConstraintsForSecondActionButton);
            //this.AddConstraints(vConstraintsForActivityIndicatorView);
            //this.AddConstraints(hConstraintsForActivityIndicatorView);
        }