UISlider在iOS App中增加了滑块尺寸但没有拇指扭曲

时间:2017-08-15 23:07:05

标签: xamarin xamarin.ios xamarin.forms

我需要增加使用iOS app创建的Xamarin Forms中滑块的大小。我尝试使用以下自定义渲染器覆盖 LayoutSubviews 。它增加了尺寸,但拇指的尺寸增加是不可接受的。如何减小拇指的大小,保持滑块的高度增加?

在Android中可以显示:Increase height of slider without affecting thumb in Android App

iOS参考:

  1. Custom UISlider - Increase "hot spot" size

  2. Increasing a thumbs "click area" in UISlider

  3. C#自定义渲染器

    [assembly: ExportRenderer(typeof(MySlider), typeof(MySliderRenderer))]
    namespace CustomRenderer.iOS
    {
        class MySliderRenderer : Xamarin.Forms.Platform.iOS.SliderRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs<Slider> e)
            {
                base.OnElementChanged(e);   
                Control.MinimumTrackTintColor = UIColor.FromRGB(0xE6, 0x00, 0x06);
                Control.ThumbTintColor = UIColor.Red;
                Control.MinimumTrackTintColor = UIColor.Orange;
                Control.MaximumTrackTintColor = UIColor.Yellow;
                //Control.SetThumbImage(UIImage.FromFile("myicon.png"), UIControlState.Normal);
            }
    
            public override void LayoutSubviews()
            {
                base.LayoutSubviews();
                CGAffineTransform transform = CGAffineTransform.MakeScale(1f, 12.0f);
                transform.TransformSize(this.Frame.Size);
                this.Control.Transform = transform;            
            }
        }
    }
    

    enter image description here

    更新 - 参考 - 渲染器

    1. Customizing Control Rendering in Xamarin.Forms

    2. Implementing a View - Rendering a platform-specific view

    3. iOS range slider for Xamarin and Xamarin.Forms

    4. Fullscreen Video Background Control for Xamarin.Forms

1 个答案:

答案 0 :(得分:5)

覆盖TrackRectForBounds以更改其高度,而不是更改transform

在MySliderRenderer中

class MySliderRenderer : SliderRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Slider> e)
    {
        SetNativeControl(new MySlideriOS());
        base.OnElementChanged(e);
    }
}

创建UISlider的子类

public class MySlideriOS : UISlider
{
    public MySlideriOS() {
        this.MinimumTrackTintColor = UIColor.FromRGB(0xE6, 0x00, 0x06);
        this.ThumbTintColor = UIColor.Red;
        this.MinimumTrackTintColor = UIColor.Orange;
        this.MaximumTrackTintColor = UIColor.Yellow;
    }

    public override CGRect TrackRectForBounds(CGRect forBounds)
    {
        CGRect rect =  base.TrackRectForBounds(forBounds);
        return new CGRect(rect.X, rect.Y, rect.Width, 20);
    }
}

我的测试

enter image description here

这是Sample