Skip to content

Commit

Permalink
fix: Make sure parent's shadow children collection is up-to-date in c…
Browse files Browse the repository at this point in the history
…hild.Loaded
  • Loading branch information
dr1rrb committed Jan 26, 2021
1 parent f029e37 commit 218f660
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/Uno.UI/Controls/BindableUIView.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,34 @@ public override void SubviewAdded(UIView uiview)

internal List<UIView>.Enumerator GetChildrenEnumerator() => _shadowChildren.Materialized.GetEnumerator();

public override void WillRemoveSubview(UIView uiview)
public override void AddSubview(UIView view)
{
base.WillRemoveSubview(uiview);
// As iOS will invoke the MovedToWindow on the 'view' (which will invoke the Loaded event)
// ** before ** invoking the SubviewAdded on its parent (i.e. this element),
// we cannot rely only on the cloned collection made in that SubviewAdded callback.
// Instead we have to pre-update the _shadowChildren so handlers of the Loaded event will be able
// to properly walk the tree up and down (cf. EffectiveViewport).
_shadowChildren.Add(view);
base.AddSubview(view);
}

var position = _shadowChildren.IndexOf(uiview, ReferenceEqualityComparer<UIView>.Default);
public override void InsertSubview(UIView view, nint atIndex)
{
// cf. AddSubview comment!
_shadowChildren.Insert((int)atIndex, view);
base.InsertSubview(view, atIndex);
}

if(position != -1)
public override void WillRemoveSubview(UIView uiview)
{
// cf. AddSubview comment!
var index = _shadowChildren.IndexOf(uiview, ReferenceEqualityComparer<UIView>.Default);
if (index != -1)
{
_shadowChildren.RemoveAt(position);
_shadowChildren.RemoveAt(index);
}

base.WillRemoveSubview(uiview);
}

public BindableUIView()
Expand Down

0 comments on commit 218f660

Please sign in to comment.