UIProgressView 在设备旋转时消失或不缩放

UIProgressView disappear or doesn't scale when device rotate

我正在使用 Xamarin.IOS,我在带有两个按钮的 UIScrollView 中有 UIprogressView。

我在 ViewWillLayoutSubviews 方法中通过代码设置 UIprogressView 框架:

        CGRect newFrame;
        newFrame.Width = MyScrollView.Frame.Width / 2;
        newFrame.Location = new CGPoint(0, NextButton.Frame.Y);
        MyProgressView.Frame = newFrame;
        MyProgressView.Transform = CGAffineTransform.MakeScale(1, 20f);
        MyProgressView.TrackTintColor = CustomColors.CustomColors.GetColor(CustomColors.CustomColors.ColorGray);
        MyProgressView.ProgressTintColor = CustomColors.CustomColors.GetColor(CustomColors.CustomColors.ColorBlue);

问题是当我将设备从纵向旋转到横向时,只有进度视图会消失。同时,下一个按钮在那里,进度视图和下一个按钮都有相同的 Y!。

如果我停止使用 MyProgressView.ProgressTintColor 我可以看到旋转后的进度视图,但它没有缩放。

知道如何解决吗?

这就是我说的 UIProgressView:

在您的 Helper 文件夹中创建此 UIProgressView 文件

LoadingOverlay.cs

public class LoadingOverlay : UIView
    {
        // control declarations
        UIActivityIndicatorView activitySpinner;
        UILabel loadingLabel;

    public LoadingOverlay(CGRect frame) : base(frame)
    {
        // configurable bits
        BackgroundColor = UIColor.Black;
        Alpha = 0.75f;
        AutoresizingMask = UIViewAutoresizing.All;

        nfloat labelHeight = 22;
        nfloat labelWidth = Frame.Width - 20;

        // derive the center x and y
        nfloat centerX = Frame.Width / 2;
        nfloat centerY = Frame.Height / 2;

        // create the activity spinner, center it horizontall and put it 5 points above center x
        activitySpinner = new UIActivityIndicatorView(UIActivityIndicatorViewStyle.WhiteLarge);
        activitySpinner.Frame = new CGRect(
            centerX - (activitySpinner.Frame.Width / 2),
            centerY - activitySpinner.Frame.Height - 20,
            activitySpinner.Frame.Width,
            activitySpinner.Frame.Height);
        activitySpinner.AutoresizingMask = UIViewAutoresizing.All;
        AddSubview(activitySpinner);
        activitySpinner.StartAnimating();

        // create and configure the "Loading Data" label
        loadingLabel = new UILabel(new CGRect(
            centerX - (labelWidth / 2),
            centerY + 20,
            labelWidth,
            labelHeight
            ));
        loadingLabel.BackgroundColor = UIColor.Clear;
        loadingLabel.TextColor = UIColor.White;
        loadingLabel.Text = "Loading Data...";
        loadingLabel.TextAlignment = UITextAlignment.Center;
        loadingLabel.AutoresizingMask = UIViewAutoresizing.All;
        AddSubview(loadingLabel);

    }

    /// <summary>
    /// Fades out the control and then removes it from the super view
    /// </summary>
    public void Hide()
    {
        UIView.Animate(
            0.5, // duration
            () => { Alpha = 0; },
            () => { RemoveFromSuperview(); }
        );
    }
}

并以这种方式使用

显示正在加载

var loadingOverlay = new LoadingOverlay(UIScreen.MainScreen.Bounds);
View.Add(loadingOverlay);

并隐藏它

loadingOverlay.Hide();

我通过在每次旋转时重新缩放来修复它:

public override void WillAnimateRotation(UIInterfaceOrientation toInterfaceOrientation, double duration)
{
     progressView.Transform = CGAffineTransform.Scale(progressView.Transform,1, 20f);   
}