diff options
author | Jason Smith <jason.smith@xamarin.com> | 2016-03-22 13:02:25 -0700 |
---|---|---|
committer | Jason Smith <jason.smith@xamarin.com> | 2016-03-22 16:13:41 -0700 |
commit | 17fdde66d94155fc62a034fa6658995bef6fd6e5 (patch) | |
tree | b5e5073a2a7b15cdbe826faa5c763e270a505729 /Xamarin.Forms.Platform.iOS/Extensions/UIViewExtensions.cs | |
download | xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.tar.gz xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.tar.bz2 xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.zip |
Initial import
Diffstat (limited to 'Xamarin.Forms.Platform.iOS/Extensions/UIViewExtensions.cs')
-rw-r--r-- | Xamarin.Forms.Platform.iOS/Extensions/UIViewExtensions.cs | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.iOS/Extensions/UIViewExtensions.cs b/Xamarin.Forms.Platform.iOS/Extensions/UIViewExtensions.cs new file mode 100644 index 00000000..9847867c --- /dev/null +++ b/Xamarin.Forms.Platform.iOS/Extensions/UIViewExtensions.cs @@ -0,0 +1,66 @@ +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System; +#if __UNIFIED__ +using UIKit; + +#else +using MonoTouch.UIKit; +#endif + +namespace Xamarin.Forms.Platform.iOS +{ + public static class UIViewExtensions + { + public static IEnumerable<UIView> Descendants(this UIView self) + { + if (self.Subviews == null) + return Enumerable.Empty<UIView>(); + return self.Subviews.Concat(self.Subviews.SelectMany(s => s.Descendants())); + } + + public static SizeRequest GetSizeRequest(this UIView self, double widthConstraint, double heightConstraint, double minimumWidth = -1, double minimumHeight = -1) + { + var s = self.SizeThatFits(new SizeF((float)widthConstraint, (float)heightConstraint)); + var request = new Size(s.Width == float.PositiveInfinity ? double.PositiveInfinity : s.Width, s.Height == float.PositiveInfinity ? double.PositiveInfinity : s.Height); + var minimum = new Size(minimumWidth < 0 ? request.Width : minimumWidth, minimumHeight < 0 ? request.Height : minimumHeight); + return new SizeRequest(request, minimum); + } + + internal static T FindDescendantView<T>(this UIView view) where T : UIView + { + var queue = new Queue<UIView>(); + queue.Enqueue(view); + + while (queue.Count > 0) + { + var descendantView = queue.Dequeue(); + + var result = descendantView as T; + if (result != null) + return result; + + for (var i = 0; i < descendantView.Subviews.Length; i++) + queue.Enqueue(descendantView.Subviews[i]); + } + + return null; + } + + internal static UIView FindFirstResponder(this UIView view) + { + if (view.IsFirstResponder) + return view; + + foreach (var subView in view.Subviews) + { + var firstResponder = subView.FindFirstResponder(); + if (firstResponder != null) + return firstResponder; + } + + return null; + } + } +}
\ No newline at end of file |