Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

iOS Loading Overlay

$
0
0

I attempted to implement the loading overlay found here: http://docs.xamarin.com/recipes/ios/standard_controls/popovers/display_a_loading_message/ That being said, I attempted to implement this along side a web view because the web view was taking too long to load certain web pages. I have pasted an example of my code below. Now the code works with a number of pages just fine but several others appear to keep the overlay up and never hide it even though the page appears to have fully loaded. I have an example below with the cartoonnetwork.com main page. This page appears to load but the overlay keeps displaying even after LoadFinished has kicked off. Does anyone have any experience using this or something similar to display a type of loading screen? I thought about perhaps using the DispatchAfter method to set a timer in case it doesn't dismiss the overlay but that would defeat the purpose of having the overlay that auto detects on finish.

Any advice or ideas on how I could get something working for my needs would be helpful.

For simplicity I thought I would also include the LoadingOverlay class code from the recipe listed above.

 public override void ViewDidLoad ()
 {
      base.ViewDidLoad ();
      LoadingOverlay loadingOverlay;
      NSUrl url = new NSUrl ("http://www.cartoonnetwork.com/video/index.html");  
      NSMutableUrlRequest req = new NSMutableUrlRequest (url);

      //When the web view starts to load
      this.WebView.LoadStarted += (object sender, EventArgs e) => {
           loadingOverlay = new LoadingOverlay(this.WebView.Bounds);
           this.WebView.Add(loadingOverlay);
           UIApplication.SharedApplication.NetworkActivityIndicatorVisible = true;
      };

      //When the web view is finished loading
      this.WebView.LoadFinished += (object sender, EventArgs e) => {
           loadingOverlay.Hide();
           UIApplication.SharedApplication.NetworkActivityIndicatorVisible = false;
      };

      //open the webview with the desired request
      this.WebView.LoadRequest (req);
 }

//Overlay class

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

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

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

        // derive the center x and y
        float centerX = Frame.Width / 2;
        float 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 RectangleF (
            centerX - (activitySpinner.Frame.Width / 2) ,
            centerY - activitySpinner.Frame.Height - 20 ,
            activitySpinner.Frame.Width ,
            activitySpinner.Frame.Height);
        activitySpinner.AutoresizingMask = UIViewAutoresizing.FlexibleMargins;
        AddSubview (activitySpinner);
        activitySpinner.StartAnimating ();

        // create and configure the "Loading Data" label
        loadingLabel = new UILabel(new RectangleF (
            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.FlexibleMargins;
        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(); }
        );
    }
};

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>