You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin.Forms iOS自定义高度ProgressBar创建失败问题求助

嘿,关于你在最新版Xamarin.Forms里没法创建自定义高度的iOS ProgressBar的问题,我帮你梳理下解决方案,结合你给出的代码片段来完善:

解决iOS自定义高度ProgressBar的方案

首先,你已经创建了自定义的SplashScreenProgressBar类,但代码看起来没写完,我先帮你补全HeightExtended绑定属性,这样咱们能在XAML或代码里直接设置高度:

public class SplashScreenProgressBar : ProgressBar 
{
    // 进度条前景色属性
    public static readonly BindableProperty TintColorProperty = 
        BindableProperty.Create(nameof(TintColor), typeof(Color), typeof(SplashScreenProgressBar), Color.Green);

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

    // 自定义高度属性
    public static readonly BindableProperty HeightExtendedProperty = 
        BindableProperty.Create(nameof(HeightExtended), typeof(double), typeof(SplashScreenProgressBar), 2.0);

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

关键:iOS自定义渲染器

Xamarin.Forms的默认ProgressBar在iOS上依赖原生UIProgressView,它的高度是固定的,所以咱们需要写一个自定义渲染器来修改高度,同时处理你定义的TintColor属性:

[assembly: ExportRenderer(typeof(SplashScreenProgressBar), typeof(SplashScreenProgressBarRenderer))]
namespace YourAppName.iOS.Renderers // 替换成你的iOS项目实际命名空间
{
    public class SplashScreenProgressBarRenderer : ProgressBarRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<ProgressBar> e)
        {
            base.OnElementChanged(e);

            if (Control != null && Element is SplashScreenProgressBar customBar)
            {
                // 设置进度条前景色
                Control.ProgressTintColor = customBar.TintColor.ToUIColor();
                // 可选:设置进度条背景轨道颜色
                Control.TrackTintColor = customBar.BackgroundColor.ToUIColor();

                // 自定义高度:通过缩放Y轴实现(简单直接)
                var targetHeight = (nfloat)customBar.HeightExtended;
                var defaultHeight = Control.Frame.Height;
                var scaleY = targetHeight / defaultHeight;
                Control.Transform = CGAffineTransform.MakeScale(1.0f, scaleY);

                // 若遇到布局问题,推荐用约束方式替代缩放:
                // Control.TranslatesAutoresizingMaskIntoConstraints = false;
                // Control.HeightAnchor.ConstraintEqualTo(targetHeight).Active = true;
            }
        }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            if (Control == null || !(Element is SplashScreenProgressBar customBar)) return;

            // 当TintColor属性变化时更新颜色
            if (e.PropertyName == SplashScreenProgressBar.TintColorProperty.PropertyName)
            {
                Control.ProgressTintColor = customBar.TintColor.ToUIColor();
            }
            // 当HeightExtended属性变化时更新高度
            else if (e.PropertyName == SplashScreenProgressBar.HeightExtendedProperty.PropertyName)
            {
                var targetHeight = (nfloat)customBar.HeightExtended;
                var defaultHeight = Control.Frame.Height;
                var scaleY = targetHeight / defaultHeight;
                Control.Transform = CGAffineTransform.MakeScale(1.0f, scaleY);
            }
        }
    }
}

在XAML里使用自定义ProgressBar

现在你可以直接在XAML中引用这个自定义控件,设置高度和颜色了:

<!-- 记得先引入自定义控件的命名空间 -->
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:YourAppName" <!-- 替换成你的自定义控件命名空间 -->
             x:Class="YourAppName.MainPage">

    <StackLayout Padding="20" VerticalOptions="Center">
        <local:SplashScreenProgressBar 
            Progress="0.6"
            TintColor="#2563EB"
            HeightExtended="10"
            BackgroundColor="#E5E7EB"
            Margin="0 10"/>
    </StackLayout>
</ContentPage>

注意事项

  • 确保iOS项目中已经添加了这个渲染器类,并且ExportRenderer特性的命名空间和控件类型都正确
  • 如果用约束方式设置高度,要确保父布局支持自动布局(比如StackLayout、Grid),避免出现布局异常
  • 测试时一定要在iOS模拟器或真机上运行,Android的自定义ProgressBar逻辑和iOS不同哦

内容的提问来源于stack exchange,提问作者Atlantis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:31:23