How to make my RefreshControl appear during ViewDidAppear

I myself answered this question - leaving it here for people with the same problem.

My code is pretty simple - a custom table with a data source to provide data from the web service asynchronously.

To get the most enjoyable user interface, I would like UIRefreshControl animate the boot process whenever this controller appears, and not just when the list has been unchecked.

Unfortunately, UIRefreshControl is not displayed at all if I call my method during ViewDidAppear .

I tried the suggestions from the answers to these questions, but none of them seemed to work for me:

 public partial class ControlCenterSelectionController : UITableViewController { public ControlCenterSelectionController (IntPtr handle) : base (handle) { } public override void ViewDidLoad() { base.ViewDidLoad(); TableView.RefreshControl = new UIRefreshControl(); TableView.RefreshControl.ValueChanged += RefreshControlOnValueChanged; } private async void RefreshControlOnValueChanged(object sender, EventArgs eventArgs) { await UpdateDataSourceAsync(); } public override async void ViewDidAppear(bool animated) { base.ViewDidAppear(animated); TableView.SetContentOffset(new CGPoint(0, -TableView.RefreshControl.Frame.Size.Height), true); await UpdateDataSourceAsync(); } private async Task UpdateDataSourceAsync() { TableView.RefreshControl.BeginRefreshing(); var ccClient = DI.Instance.Get<IControlCenterRestClient>(); var controlCenters = await ccClient.GetAllAsync(); var source = new GenericViewSource<ControlCenter>(controlCenters, item => item.Name, ControlCenterSelected); TableView.Source = source; TableView.RefreshControl.EndRefreshing(); } private void ControlCenterSelected(ControlCenter controlCenter) { var controller = this.InstantiateController(Constants.ViewControllerIds.ControlCenter) as ControlCenterController; controller.ControlCenter = controlCenter; this.NavigateTo(controller); } } 

Currently, I get a screen that looks blank, loads data without instructions, and then refreshes the screen after it is executed.

Can someone spot an error? I can not find it, since this code is pretty simple.

0
source share
1 answer

The desperation of sending it to SO + desperate coding attempts were key:

  public override async void ViewDidAppear(bool animated) { base.ViewDidAppear(animated); TableView.SetContentOffset(new CGPoint(0, -(TableView.RefreshControl.Frame.Size.Height + 20)), true); TableView.RefreshControl.BeginRefreshing(); await UpdateDataSourceAsync(); } private async Task UpdateDataSourceAsync() { var ccClient = DI.Instance.Get<IControlCenterRestClient>(); var controlCenters = await ccClient.GetAllAsync(); var source = new GenericViewSource<ControlCenter>(controlCenters, item => item.Name, ControlCenterSelected); TableView.Source = source; TableView.RefreshControl.EndRefreshing(); } 

This change, in particular, made a difference:

 -(TableView.RefreshControl.Frame.Size.Height + 20) 

It turns out that while in other issues the problem was fixed by simply applying scrolling based on RefreshControl, it would catch fire in the animation, in my case I had to add an extra delta to create the animation.

Correction

this additional offset is apparently only required if the extended edges are set in the upper bars.

0
source

Source: https://habr.com/ru/post/1487054/


All Articles