private void SetupNavBar(CGSize size)
        {
            if (NavigationController != null && titleView != null)
            {
                var page = Element as Page;
                containerView.Frame = new CGRect(0, 0, size.Width, size.Height);


                titleView.Layer.BorderWidth = ExtendedNavigationPage.GetTitleBorderWidth(Element);

                titleView.Layer.CornerRadius = ExtendedNavigationPage.GetTitleBorderCornerRadius(Element);

                titleView.Layer.BorderColor = ExtendedNavigationPage.GetTitleBorderColor(Element)?.ToCGColor() ?? UIColor.Clear.CGColor;


                SetupTextFont(titleLabel, ExtendedNavigationPage.GetTitleFont(page), ExtendedNavigationPage.GetTitleColor(page));

                SetupBackground();

                if (!string.IsNullOrEmpty(ExtendedNavigationPage.GetTitleBackground(Element)))
                {
                    try
                    {
                        var image = UIImage.FromBundle(ExtendedNavigationPage.GetTitleBackground(Element));
                        titleView.Frame = new CGRect(titleView.Frame.X, titleView.Frame.Y, titleView.Frame.Width == 0?Math.Min(size.Width, image.Size.Width): Math.Min(titleView.Frame.Width, image.Size.Width), titleView.Frame.Height == 0 ? Math.Min(size.Height, image.Size.Height) : Math.Min(titleView.Frame.Height, image.Size.Height));

                        titleView.BackgroundColor = UIColor.FromPatternImage(image);
                    }
                    catch (Exception ex)
                    {
                        titleView.BackgroundColor = ExtendedNavigationPage.GetTitleFillColor(Element)?.ToUIColor() ?? UIColor.Clear;
                    }
                }
                else
                {
                    titleView.BackgroundColor = ExtendedNavigationPage.GetTitleFillColor(Element)?.ToUIColor() ?? UIColor.Clear;
                }


                ParentViewController.NavigationItem.TitleView = containerView;
                ParentViewController.NavigationItem.TitleView.SetNeedsDisplay();
            }
        }
        private void Element_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            var page = sender as Page;

            System.Diagnostics.Debug.WriteLine(e.PropertyName);

            switch (e.PropertyName)
            {
            case Page.TitleProperty.PropertyName:
            case ExtendedNavigationPage.TitleFontProperty.PropertyName:
            case CustomPage.SubtitleProperty.PropertyName:
            case ExtendedNavigationPage.SubtitleFontProperty.PropertyName:
                SetupTextFont(titleLabel, ExtendedNavigationPage.GetTitleFont(page), ExtendedNavigationPage.GetTitleColor(page));

                SetTitlePosition(ExtendedNavigationPage.GetTitlePosition(page), ExtendedNavigationPage.GetTitlePadding(Element), ExtendedNavigationPage.GetTitleMargin(Element), new CGRect(0, 0, Math.Max(subtitleLabel.IntrinsicContentSize.Width, titleLabel.IntrinsicContentSize.Width), (titleLabel.IntrinsicContentSize.Height + subtitleLabel.IntrinsicContentSize.Height + (subtitleLabel.IntrinsicContentSize.Height > 0.0f ? 3.0f : 0.0f))));
                break;

            case ExtendedNavigationPage.TitleColorProperty.PropertyName:
                var titleColor = ExtendedNavigationPage.GetTitleColor(page);
                if (titleColor.HasValue)
                {
                    titleLabel.TextColor = titleColor.Value.ToUIColor();
                }
                break;

            case ExtendedNavigationPage.SubtitleColorProperty.PropertyName:
                var subtitleColor = ExtendedNavigationPage.GetSubtitleColor(page);
                if (subtitleColor.HasValue)
                {
                    subtitleLabel.TextColor = subtitleColor.Value.ToUIColor();
                }
                break;

            case ExtendedNavigationPage.TitlePositionProperty.PropertyName:
            case ExtendedNavigationPage.TitlePaddingProperty.PropertyName:
            case ExtendedNavigationPage.TitleMarginProperty.PropertyName:
                SetTitlePosition(ExtendedNavigationPage.GetTitlePosition(Element), ExtendedNavigationPage.GetTitlePadding(Element), ExtendedNavigationPage.GetTitleMargin(Element), new CGRect(0, 0, Math.Max(subtitleLabel.IntrinsicContentSize.Width, titleLabel.IntrinsicContentSize.Width), (titleLabel.IntrinsicContentSize.Height + subtitleLabel.IntrinsicContentSize.Height + (subtitleLabel.IntrinsicContentSize.Height > 0.0f ? 3.0f : 0.0f))));
                break;

            case ExtendedNavigationPage.GradientColorsProperty.PropertyName:
            case ExtendedNavigationPage.GradientDirectionProperty.PropertyName:
            case ExtendedNavigationPage.BarBackgroundProperty.PropertyName:
            case ExtendedNavigationPage.BarBackgroundOpacityProperty.PropertyName:
                SetupBackground();
                break;

            case ExtendedNavigationPage.HasShadowProperty.PropertyName:
                SetupShadow(ExtendedNavigationPage.GetHasShadow(page));
                break;

            case ExtendedNavigationPage.TitleBackgroundProperty.PropertyName:
                if (!string.IsNullOrEmpty(ExtendedNavigationPage.GetTitleBackground(Element)))
                {
                    titleView.BackgroundColor = UIColor.FromPatternImage(UIImage.FromBundle(ExtendedNavigationPage.GetTitleBackground(Element)));
                }
                else
                {
                    titleView.BackgroundColor = null;
                }
                break;

            case nameof(ExtendedNavigationPage.TitleBorderWidth):
                titleView.Layer.BorderWidth = ExtendedNavigationPage.GetTitleBorderWidth(Element);
                break;

            case nameof(ExtendedNavigationPage.TitleBorderCornerRadius):
                titleView.Layer.CornerRadius = ExtendedNavigationPage.GetTitleBorderCornerRadius(Element);
                break;

            case nameof(ExtendedNavigationPage.TitleBorderColor):
                titleView.Layer.BorderColor = ExtendedNavigationPage.GetTitleBorderColor(Element)?.ToCGColor() ?? UIColor.Clear.CGColor;
                break;

            case nameof(ExtendedNavigationPage.TitleFillColor):
                titleView.BackgroundColor = ExtendedNavigationPage.GetTitleFillColor(Element)?.ToUIColor() ?? UIColor.Clear;
                break;

            case nameof(CustomPage.FormattedTitle):
                if (page is ContentPage cPage)
                {
                    SetupFormattedText(titleLabel, CustomPage.GetFormattedTitle(cPage), cPage.Title);
                }
                break;
            }
        }
        private void SetTitlePosition(TitleAlignment alignment, Thickness padding, Thickness margin, CGRect vFrame)
        {
            var marginX       = margin.Top;
            var marginY       = margin.Left;
            var marginWidth   = margin.Left + margin.Right;
            var marginHeight  = margin.Top + margin.Bottom;
            var paddingWidth  = padding.Left + padding.Right;
            var paddingHeight = padding.Top + padding.Bottom;
            var paddingX      = padding.Left;
            var paddingY      = padding.Top;

            if (ExtendedNavigationPage.GetTitleBackground(Element) != null && vFrame.Width == 0 && vFrame.Height == 0)
            {
                vFrame = titleView.Frame;
            }

            marginView.Frame = new CGRect(vFrame.X, vFrame.Y, vFrame.Width, vFrame.Height);

            double offset = 0;

            titleLabel.AutoresizingMask = UIViewAutoresizing.All;
            switch (alignment)
            {
            case TitleAlignment.Start:
                marginView.Frame = new CGRect(vFrame.X, marginView.Frame.Y, marginView.Bounds.Width + marginWidth + paddingWidth, marginView.Bounds.Height + marginHeight + paddingHeight);
                var startCenter = marginView.Center;
                startCenter.Y               = marginView.Superview.Center.Y;
                marginView.Center           = startCenter;
                titleLabel.TextAlignment    = UITextAlignment.Left;
                subtitleLabel.TextAlignment = UITextAlignment.Left;
                offset = marginX;
                break;

            case TitleAlignment.Center:
                offset                      = marginX;
                marginView.Frame            = new CGRect(marginView.Frame.X, marginView.Frame.Y, marginView.Bounds.Width + marginWidth + paddingWidth, marginView.Bounds.Height + marginHeight + paddingHeight);
                marginView.Center           = marginView.Superview.Center;
                titleLabel.TextAlignment    = UITextAlignment.Center;
                subtitleLabel.TextAlignment = UITextAlignment.Center;
                break;

            case TitleAlignment.End:

                var endCenter = marginView.Center;
                endCenter.Y       = marginView.Superview.Center.Y;
                marginView.Center = endCenter;

                titleLabel.TextAlignment    = UITextAlignment.Right;
                subtitleLabel.TextAlignment = UITextAlignment.Right;
                marginView.Frame            = new CGRect(marginView.Superview.Frame.Width - marginView.Frame.Width - offset - marginWidth - paddingWidth, marginView.Frame.Y, marginView.Bounds.Width + marginWidth + paddingWidth, marginView.Bounds.Height + marginHeight + paddingHeight);
                offset = marginView.Frame.Width - vFrame.Width - paddingWidth - marginX;
                break;
            }

            titleView.Frame = new CGRect(offset, vFrame.Y + marginY, vFrame.Width + paddingWidth, vFrame.Height + paddingHeight);

            var cPage             = Element as ContentPage;
            var formattedSubtitle = CustomPage.GetFormattedSubtitle(Element);
            var subtitle          = CustomPage.GetFormattedSubtitle(Element);

            if (cPage != null && (!string.IsNullOrEmpty(subtitle) ||
                                  (formattedSubtitle != null && formattedSubtitle.Spans.Count > 0)))
            {
                titleLabel.Frame = new CGRect(paddingX, paddingY, titleView.Frame.Width, titleLabel.IntrinsicContentSize.Height);

                subtitleLabel.Frame = new CGRect(titleLabel.Frame.X, titleLabel.Frame.Y + titleLabel.Frame.Height + 3, titleView.Frame.Width, subtitleLabel.Frame.Height);
            }
            else
            {
                titleLabel.Frame = new CGRect(paddingX, paddingY, titleLabel.IntrinsicContentSize.Width, titleLabel.IntrinsicContentSize.Height);
            }
        }