我需要增加使用iOS app
创建的Xamarin Forms
中滑块的大小。我尝试使用以下自定义渲染器覆盖 LayoutSubviews
。它增加了尺寸,但拇指的尺寸增加是不可接受的。如何减小拇指的大小,保持滑块的高度增加?
在Android中可以显示:Increase height of slider without affecting thumb in Android App
iOS参考:
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;
}
}
}
更新 - 参考 - 渲染器
答案 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);
}
}
我的测试
这是Sample