using System; using System.Drawing; using System.ComponentModel; #if __UNIFIED__ using UIKit; #else using MonoTouch.UIKit; #endif #if __UNIFIED__ using RectangleF = CoreGraphics.CGRect; using SizeF = CoreGraphics.CGSize; using PointF = CoreGraphics.CGPoint; #else using nfloat=System.Single; using nint=System.Int32; using nuint=System.UInt32; #endif namespace Xamarin.Forms.Platform.iOS { public class SliderRenderer : ViewRenderer { SizeF _fitSize; public override SizeF SizeThatFits(SizeF size) { return _fitSize; } protected override void Dispose(bool disposing) { if (Control != null) Control.ValueChanged -= OnControlValueChanged; base.Dispose(disposing); } protected override void OnElementChanged(ElementChangedEventArgs e) { if (e.NewElement != null) { if (Control == null) { SetNativeControl(new UISlider { Continuous = true }); Control.ValueChanged += OnControlValueChanged; // sliders SizeThatFits method returns non-useful answers // this however gives a very useful answer Control.SizeToFit(); _fitSize = Control.Bounds.Size; // except if your not running iOS 7... then it fails... if (_fitSize.Width <= 0 || _fitSize.Height <= 0) _fitSize = new SizeF(22, 22); // Per the glorious documentation known as the SDK docs } UpdateMaximum(); UpdateMinimum(); UpdateValue(); } base.OnElementChanged(e); } protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { base.OnElementPropertyChanged(sender, e); if (e.PropertyName == Slider.MaximumProperty.PropertyName) UpdateMaximum(); else if (e.PropertyName == Slider.MinimumProperty.PropertyName) UpdateMinimum(); else if (e.PropertyName == Slider.ValueProperty.PropertyName) UpdateValue(); } void OnControlValueChanged(object sender, EventArgs eventArgs) { ((IElementController)Element).SetValueFromRenderer(Slider.ValueProperty, Control.Value); } void UpdateMaximum() { Control.MaxValue = (float)Element.Maximum; } void UpdateMinimum() { Control.MinValue = (float)Element.Minimum; } void UpdateValue() { if ((float)Element.Value != Control.Value) Control.Value = (float)Element.Value; } } }