예제 #1
0
        protected override void OnElementChanged(ElementChangedEventArgs <SegmentView> e)
        {
            base.OnElementChanged(e);

            if (e.NewElement == null)
            {
                return;
            }

            if (Control == null)
            {
                _control = new UISegmentedControl();

                for (int i = 0; i < Element.Children.Count; i++)
                {
                    var segment = Element.Children.ElementAt(i);
                    if (segment.Image != null)
                    {
                        var img = NativeImage.FromFile(((FileImageSource)segment.Image).File);
                        img = img.Scale(new CoreGraphics.CGSize(17, 17)); // Apple docs
                        _control.InsertSegment(img, i, false);
                    }
                    else
                    {
                        _control.InsertSegment(segment.Title, i, false);
                    }
                }

                _control.ClipsToBounds       = true;
                _control.TintColor           = Element.TintColor.ToUIColor();
                _control.SelectedSegment     = Element.SelectedIndex;
                _control.BackgroundColor     = Element.BackgroundColor.ToUIColor();
                _control.Layer.MasksToBounds = true;

                _control.Layer.BorderColor = Element.IsBorderColorSet()
                    ? Element.BorderColor.ToCGColor()
                    : Element.TintColor.ToCGColor();

                if (Element.IsCornerRadiusSet())
                {
                    _control.Layer.CornerRadius = Element.CornerRadius;
                    _control.Layer.BorderWidth  = (nfloat)Element.BorderWidth;
                }

                SetSelectedTextColor();
                SetNativeControl(_control);
            }

            if (e.OldElement != null && _control != null)
            {
                _control.ValueChanged -= OnSelectedIndexChanged;
            }

            if (e.NewElement != null)
            {
                _control.ValueChanged += OnSelectedIndexChanged;
            }
        }