Xamarin.Form iOS自定义高度ProgressBar问题

时间:2018-03-27 16:33:34

标签: xamarin xamarin.forms xamarin.ios

更新:

因此,我无法创建IOS自定义高度ProgressBar。 我使用最新版本的Xamarin.Forms。

.cs文件:

public class SplashScreenProgressBar : ProgressBar
{
    public static readonly BindableProperty TintColorProperty =
        BindableProperty.Create<CustomProgressBar, Color>( p => p.TintColor, Color.Green);

    public Color TintColor
    {
        get { return (Color) GetValue(TintColorProperty); }
        set { SetValue(TintColorProperty, value); }
    }

    public static readonly BindableProperty HeightExtendedProperty =
        BindableProperty.Create("HeightExtended", typeof(double), typeof(SplashScreenProgressBar), 10.0);

    public double HeightExtended
    {
        get { return (double) GetValue(HeightExtendedProperty); }
        set { SetValue(HeightExtendedProperty, value); }
    }

    public static readonly BindableProperty BackgroundColorExtendedProperty =
        BindableProperty.Create("BackgroundColorExtended", typeof(Color), typeof(SplashScreenProgressBar),
            Color.White);

    public Color BackgroundColorExtended
    {
        get { return (Color) GetValue(BackgroundColorExtendedProperty); }
        set { SetValue(BackgroundColorExtendedProperty, value); }
    }
}

这是iOS渲染器:

public class SplashScreenProgressBarRenderer : ProgressBarRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<ProgressBar> e)
    {
        base.OnElementChanged(e);
        var element = (SplashScreenProgressBar)Element;
        this.Control.ProgressTintColor = element.TintColor.ToUIColor();
        this.Control.TrackTintColor = element.BackgroundColorExtended.ToUIColor();
    }

    public override void LayoutSubviews()
    {
        base.LayoutSubviews();

        var element = (SplashScreenProgressBar)Element;
        var X = 1.0f;
        var Y = (System.nfloat)element.HeightExtended;

        CGAffineTransform transform = CGAffineTransform.MakeScale(X, Y);

        this.Control.Transform = transform;
        this.Control.ClipsToBounds = true;
        this.Control.Layer.MasksToBounds = true;
        this.Control.CornerRadius = 5;
    }
}

xaml文件:

<StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"  BackgroundColor="White" >
        <StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Margin="50" BackgroundColor="White" >
    <views:SplashScreenProgressBar  x:Name="Progress" 
                                   TintColor="#5FA5F9" 
                                   HorizontalOptions="FillAndExpand"
                                   VerticalOptions="Center" 
                                   BackgroundColorExtended="#FFF"  />
        </StackLayout>
    </StackLayout>

但这种方式不起作用。 我用Google搜索并尝试了几乎所有我发现的样本,但没有发生任何事情。 截图: Screenshot

正如您在屏幕截图中看到的那样,半径应用于ProgressBar,但不应用高度(比例)。

Screenshot1

3 个答案:

答案 0 :(得分:0)

在PCL中

StackLayout与状态栏重叠。

在上面添加保证金。

<StackLayout Margin="50" xxxxx

在渲染器中

ClipsToBoundsLayer.MasksToBoundsLayer.CornerRadius应设置在Control而不是Renderer

this.Control.Transform = transform;
this.Control.ClipsToBounds = true;
this.Control.Layer.MasksToBounds = true;
this.Control.Layer.CornerRadius = 5;

enter image description here

答案 1 :(得分:0)

在ios中使用自定义渲染器时,它总是占据父元素的整个区域。因此,您需要在layoutsubview中再次更新进度条框架。 渲染bool;

public override void LayoutSubviews()
    {
if(!rendered)
{
       Frame = new CGRect(x,y,width,height);
       setNeedsdisplay
}
rendered=true;
    }

答案 2 :(得分:0)

所以,我花了很多时间研究和调查。 似乎有xamarin.forms iOS错误的进度条圆角。

相关问题