private void AddBackgroundAndBorderLayer(CGRect fullRect)
        {
            var backgroundLayer = new DisposableCALayer {
                Frame = fullRect
            };

            InsertScrollViewSubLayer(backgroundLayer, 0);

            var borderLayer = new DisposableCALayer {
                BackgroundColor = BorderColor.CGColor
            };

            switch (_borderType)
            {
            case DHSegmentedControlBorderType.Top:
                borderLayer.Frame = new CGRect(0, 0, fullRect.Size.Width, _borderWidth);
                break;

            case DHSegmentedControlBorderType.Left:
                borderLayer.Frame = new CGRect(0, 0, _borderWidth, fullRect.Size.Height);
                break;

            case DHSegmentedControlBorderType.Bottom:
                borderLayer.Frame = new CGRect(0, fullRect.Size.Height - _borderWidth, fullRect.Size.Width, _borderWidth);
                break;

            case DHSegmentedControlBorderType.Right:
                borderLayer.Frame = new CGRect(fullRect.Size.Width - _borderWidth, 0, _borderWidth, fullRect.Size.Height);
                break;
            }
            AddScrollViewSubLayer(borderLayer);
        }
        private void Initialize()
        {
            _previousIndex = -1;
            _scrollView    = new SegmentedScrollView {
                ScrollsToTop = false, ShowsVerticalScrollIndicator = false, ShowsHorizontalScrollIndicator = false
            };
            AddSubview(_scrollView);

            Opaque = false;
            SelectionIndicatorColor = UIColor.FromRGBA(52.0f / 255.0f, 181.0f / 255.0f, 229.0f / 255.0f, 1.0f);

            _selectedIndex   = 0;
            SegmentEdgeInset = new UIEdgeInsets(0, 0, 0, 0);

            LabelPaddingInset = new UIEdgeInsets(4, 8, 4, 8);

            SelectionIndicatorHeight      = 5.0f;
            _selectionIndicatorEdgeInsets = new UIEdgeInsets(0, 0, 0, 0);
            SelectionStyle             = DHSegmentedControlSelectionStyle.TextWidthStripe;
            SelectionIndicatorLocation = DHSegmentedControlLocation.Up;
            _segmentWidthStyle         = DHSegmentedControlWidthStyle.Fixed;
            UserDraggable          = true;
            TouchEnabled           = true;
            VerticalDividerEnabled = false;
            VerticalDividerColor   = UIColor.Black;
            BorderColor            = UIColor.Black;
            _borderWidth           = 1.0f;

            ShouldAnimateUserSelection    = true;
            _selectionIndicatorBoxOpacity = 0.2f;
            SelectionIndicatorArrowLayer  = new DisposableCALayer();
            SelectionIndicatorStripLayer  = new DisposableCALayer();
            SelectionIndicatorBoxLayer    = new DisposableCALayer {
                Opacity = _selectionIndicatorBoxOpacity, BorderWidth = 1.0f
            };

            ContentMode = UIViewContentMode.Redraw;

            _controlType = DHSegmentedControlType.Text;
        }
        public override void Draw(CGRect rect)
        {
            if (SectionTitles == null || SectionTitles.Count == 0)
            {
                return;
            }

            if (BackgroundColor != null)
            {
                BackgroundColor.SetFill();
            }

            SelectionIndicatorArrowLayer.BackgroundColor = SelectionIndicatorColor.CGColor;
            SelectionIndicatorStripLayer.BackgroundColor = SelectionIndicatorColor.CGColor;

            SelectionIndicatorBoxLayer.BackgroundColor = SelectionIndicatorColor.CGColor;
            SelectionIndicatorBoxLayer.BorderColor     = SelectionIndicatorColor.CGColor;

            _scrollView.Layer.Sublayers = new DisposableCALayer[0];
            ClearScrollViewSubLayers();
            var oldRect = rect;

            if (_controlType == DHSegmentedControlType.Text)
            {
                for (int idx = 0; idx < _sectionTitles.Count; idx++)
                {
                    var    size = MeasureTitle(idx);
                    var    stringWidth = size.Width;
                    var    stringHeight = size.Height;
                    CGRect newRect, rectDiv, rectFull;

                    var locationUp           = SelectionIndicatorLocation == DHSegmentedControlLocation.Up;
                    var selectionStyleNotBox = SelectionStyle != DHSegmentedControlSelectionStyle.Box;

                    var y = (float)Math.Round(((this.Frame.Height - (selectionStyleNotBox ? 1 : 0 * SelectionIndicatorHeight)) / 2) - (stringHeight / 2) + (SelectionIndicatorHeight * (locationUp ? 1 : 0)));

                    if (_segmentWidthStyle == DHSegmentedControlWidthStyle.Fixed && !UserDraggable)
                    {
                        stringWidth = _segmentWidth - LabelPaddingInset.Right - LabelPaddingInset.Left;

                        newRect  = new CGRect((_segmentWidth * idx) + (_segmentWidth - stringWidth) / 2, 0, stringWidth, oldRect.Height - LabelPaddingInset.Top - LabelPaddingInset.Bottom);
                        rectDiv  = new CGRect((_segmentWidth * idx) + (VerticalDividerWidth / 2), SelectionIndicatorHeight * 2, VerticalDividerWidth, Frame.Size.Height - (SelectionIndicatorHeight * 4));
                        rectFull = new CGRect(_segmentWidth * idx, 0, _segmentWidth, oldRect.Height - SelectionIndicatorHeight);
                    }
                    else if (_segmentWidthStyle == DHSegmentedControlWidthStyle.Fixed && UserDraggable)
                    {
                        newRect  = new CGRect((_segmentWidth * idx) + (_segmentWidth - stringWidth) / 2, y, stringWidth, stringHeight);
                        rectDiv  = new CGRect((_segmentWidth * idx) + (VerticalDividerWidth / 2), SelectionIndicatorHeight * 2, VerticalDividerWidth, Frame.Size.Height - (SelectionIndicatorHeight * 4));
                        rectFull = new CGRect(_segmentWidth * idx, 0, _segmentWidth, oldRect.Size.Height);
                    }
                    else
                    {
                        var xOffset = GetSelectedSegmentOffset(idx);

                        var widthForIndex = _segmentWidths[idx];
                        newRect  = new CGRect(xOffset, y, widthForIndex, stringHeight);
                        rectFull = new CGRect(_segmentWidth * idx, 0, widthForIndex, oldRect.Size.Height);
                        rectDiv  = new CGRect(xOffset - (VerticalDividerWidth / 2),
                                              SelectionIndicatorHeight * 2, VerticalDividerWidth, Frame.Size.Height - (SelectionIndicatorHeight * 4));
                    }

                    newRect = new CGRect((float)Math.Ceiling(newRect.X), (float)Math.Ceiling(newRect.Y), (float)Math.Ceiling(newRect.Size.Width), (float)Math.Ceiling(newRect.Size.Height));

                    var titleLabel = _titleLabels[idx];
                    titleLabel.AttributedText = AttributedTitle(idx);
                    titleLabel.Frame          = newRect;

                    AddScrollViewSubLayer(titleLabel.Layer);

                    if (VerticalDividerEnabled)
                    {
                        var verticalDividerLayer = new DisposableCALayer {
                            Frame = rectDiv, BackgroundColor = VerticalDividerColor.CGColor
                        };
                        AddScrollViewSubLayer(verticalDividerLayer);
                    }

                    AddBackgroundAndBorderLayer(rectFull);
                }
            }


            if (_selectedIndex != -1)
            {
                if (SelectionIndicatorStripLayer.SuperLayer == null)
                {
                    SelectionIndicatorStripLayer.Frame = FrameForSelectionIndicator();
                    AddScrollViewSubLayer(SelectionIndicatorStripLayer);

                    if (SelectionStyle == DHSegmentedControlSelectionStyle.Box && SelectionIndicatorBoxLayer.SuperLayer == null)
                    {
                        SelectionIndicatorBoxLayer.Frame = FrameForFillerSelectionIndicator();
                        InsertScrollViewSubLayer(SelectionIndicatorBoxLayer, 0);
                    }
                }
            }
        }