public static void DisposeEx(this UIView view, bool disposeView = true) { try { var viewDescription = string.Empty; var disconnectFromSuperView = true; var disposeSubviews = true; var removeGestureRecognizers = false; // WARNING: enable at your own risk, may causes crashes var removeLayerAnimations = true; var associatedViewsToDispose = new List <UIView>(); var otherDisposables = new List <IDisposable>(); if (view is UIActivityIndicatorView) { var aiv = (UIActivityIndicatorView)view; if (aiv.IsAnimating) { aiv.StopAnimating(); } } else if (view is UITableView) { var tableView = (UITableView)view; if (tableView.DataSource != null) { otherDisposables.Add(tableView.DataSource); } if (tableView.BackgroundView != null) { associatedViewsToDispose.Add(tableView.BackgroundView); } tableView.Source = null; tableView.Delegate = null; tableView.DataSource = null; tableView.WeakDelegate = null; tableView.WeakDataSource = null; associatedViewsToDispose.AddRange(tableView.VisibleCells ?? new UITableViewCell[0]); } else if (view is UITableViewCell) { var tableViewCell = (UITableViewCell)view; disposeView = false; disconnectFromSuperView = false; if (tableViewCell.ImageView != null) { associatedViewsToDispose.Add(tableViewCell.ImageView); } } else if (view is UICollectionView) { var collectionView = (UICollectionView)view; disposeView = false; if (collectionView.DataSource != null) { otherDisposables.Add(collectionView.DataSource); } associatedViewsToDispose.Add(collectionView.BackgroundView); //associatedViewsToDispose.AddRange(collectionView.VisibleCells ?? new UICollectionViewCell[0]); collectionView.Source = null; collectionView.Delegate = null; collectionView.DataSource = null; collectionView.WeakDelegate = null; collectionView.WeakDataSource = null; } else if (view is UICollectionViewCell) { var collectionViewCell = (UICollectionViewCell)view; disposeView = false; disconnectFromSuperView = false; if (collectionViewCell.BackgroundView != null) { associatedViewsToDispose.Add(collectionViewCell.BackgroundView); } } else if (view is UIWebView) { var webView = (UIWebView)view; if (webView.IsLoading) { webView.StopLoading(); } webView.LoadHtmlString(string.Empty, null); // clear display webView.Delegate = null; webView.WeakDelegate = null; } else if (view is UIImageView) { var imageView = (UIImageView)view; if (imageView.Image != null) { otherDisposables.Add(imageView.Image); imageView.Image = null; } } var gestures = view.GestureRecognizers; if (removeGestureRecognizers && gestures != null) { foreach (var gr in gestures) { view.RemoveGestureRecognizer(gr); gr.Dispose(); } } if (removeLayerAnimations && view.Layer != null) { view.Layer.RemoveAllAnimations(); } if (disconnectFromSuperView && view.Superview != null) { view.RemoveFromSuperview(); } var constraints = view.Constraints; if (constraints != null && constraints.Any() && constraints.All(c => c.Handle != IntPtr.Zero)) { view.RemoveConstraints(constraints); foreach (var constraint in constraints) { constraint.Dispose(); } } foreach (var otherDisposable in otherDisposables) { otherDisposable.Dispose(); } foreach (var otherView in associatedViewsToDispose) { otherView.DisposeEx(); } var subViews = view.Subviews; if (disposeSubviews && subViews != null) { foreach (var s in subViews) { s.DisposeEx(); } } if (disposeView) { if (view.Handle != IntPtr.Zero) { view.Dispose(); } } } catch (Exception error) { Console.Error.WriteLine(error); } }
void SetStackConstraints() { if (nativeView == null) { return; } CreateNativeViewsForChildren(); var horizontal = Orientation == StackOrientation.Horizontal; var lefts = horizontal ? children.Where(x => x.Layout.HorizontalAlignment == HorizontalAlignment.Left).ToList() : children.Where(x => x.Layout.VerticalAlignment == VerticalAlignment.Top).ToList(); var centers = horizontal ? children.Where(x => x.Layout.HorizontalAlignment == HorizontalAlignment.Center).ToList() : children.Where(x => x.Layout.VerticalAlignment == VerticalAlignment.Center).ToList(); var rights = horizontal ? children.Where(x => x.Layout.HorizontalAlignment == HorizontalAlignment.Right).ToList() : children.Where(x => x.Layout.VerticalAlignment == VerticalAlignment.Bottom).ToList(); var newConstraints = new List <NSLayoutConstraint> (); // // Align the major axis // var leftAttr = horizontal ? NSLayoutAttribute.Left : NSLayoutAttribute.Top; var rightAttr = horizontal ? NSLayoutAttribute.Right : NSLayoutAttribute.Bottom; //var centerAttr = horizontal ? NSLayoutAttribute.CenterX : NSLayoutAttribute.CenterY; Action <NativeView, NSLayoutAttribute, NativeView, NSLayoutAttribute> eq = (v1, a1, v2, a2) => { newConstraints.Add(NSLayoutConstraint.Create(v1, a1, NSLayoutRelation.Equal, v2, a2, 1, 0)); }; Child prev = null; if (lefts.Count > 0) { var first = lefts.First(); eq(nativeView, leftAttr, first.NativeView, leftAttr); prev = first; foreach (var c in lefts.Skip(1)) { eq(prev.NativeView, rightAttr, c.NativeView, leftAttr); prev = c; } } Child subs = null; if (rights.Count > 0) { var last = rights.Last(); eq(nativeView, rightAttr, last.NativeView, rightAttr); subs = last; foreach (var c in rights.Take(rights.Count - 1).Reverse()) { eq(subs.NativeView, leftAttr, c.NativeView, rightAttr); subs = c; } } if (centers.Count > 0) { throw new NotImplementedException("Center alignment not yet supported"); } // // Align the minor axis // foreach (var c in children) { var a = NSLayoutAttribute.Left; if (horizontal) { switch (c.Layout.VerticalAlignment) { case VerticalAlignment.Top: a = NSLayoutAttribute.Top; break; case VerticalAlignment.Center: a = NSLayoutAttribute.CenterY; break; default: a = NSLayoutAttribute.Bottom; break; } } else { switch (c.Layout.HorizontalAlignment) { case HorizontalAlignment.Left: a = NSLayoutAttribute.Left; break; case HorizontalAlignment.Center: a = NSLayoutAttribute.CenterX; break; default: a = NSLayoutAttribute.Right; break; } } eq(nativeView, a, c.NativeView, a); } // Swap out the old, put in the new if (constraints != null) { nativeView.RemoveConstraints(constraints); } constraints = newConstraints.ToArray(); nativeView.AddConstraints(constraints); }