Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Demo/Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">iPhoneSimulator</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{3AB2BCA1-4E9C-44FD-81AD-5B8F2B4F6BE0}</ProjectGuid>
<ProjectTypeGuids>{FEACFBD2-3405-455C-9665-78FE426C6842};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<OutputType>Exe</OutputType>
Expand Down
4 changes: 2 additions & 2 deletions Demo/TableViewCellDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override void ViewDidLoad()
{
var item = new Item();
item.Title = string.Format("Item {0}", i+1);
item.Total = r.Next(1000);
item.Total = r.Next(999)+1;
item.Count = r.Next(item.Total);
_items.Add(item);
}
Expand Down Expand Up @@ -125,7 +125,7 @@ public DemoTableViewCell() : base(UITableViewCellStyle.Default, "DemoTableViewCe
BackgroundColor = UIColor.Clear,
TextColor = UIColor.FromRGB(51,102,153),
HighlightedTextColor = UIColor.White,
Font = UIFont.BoldSystemFontOfSize(24),
Font = UIFont.BoldSystemFontOfSize(20),
TextAlignment = UITextAlignment.Right,
},
LayoutParameters = new LayoutParameters()
Expand Down
4 changes: 2 additions & 2 deletions Demo/TableViewCellDemo2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public override void ViewDidLoad()
{
var item = new Item();
item.Title = string.Format("Item {0}", i+1);
item.Total = r.Next(1000);
item.Total = r.Next(999) + 1;
item.Count = r.Next(item.Total);
item.LongText = messages[r.Next(messages.Length)];
_items.Add(item);
Expand Down Expand Up @@ -155,7 +155,7 @@ public DemoTableViewCell() : base(UITableViewCellStyle.Default, "DemoTableViewCe
BackgroundColor = UIColor.Clear,
TextColor = UIColor.FromRGB(51,102,153),
HighlightedTextColor = UIColor.White,
Font = UIFont.BoldSystemFontOfSize(24),
Font = UIFont.BoldSystemFontOfSize(20),
TextAlignment = UITextAlignment.Right,
},
LayoutParameters = new LayoutParameters()
Expand Down
24 changes: 18 additions & 6 deletions XibFree/LinearLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,16 @@ void LayoutVertical(CGRect newPosition)
// Hide hidden views
if (v.Gone)
{
v.Layout(CGRect.Empty, false);
try
{
v.Layout(CGRect.Empty, false);
}
catch
{
// mmuegel: see exception when Visibility=Gone initially; could
// not figure it out, so this *appears* to be hack work around
System.Diagnostics.Debug.WriteLine("error laying out Gone view " + v);
}
continue;
}

Expand Down Expand Up @@ -397,9 +406,12 @@ void LayoutVertical(CGRect newPosition)
x = newPosition.Right - Padding.Right - v.LayoutParameters.Margins.Right - size.Width;
break;

case Gravity.CenterHorizontal:
x = (newPosition.Left + newPosition.Right)/2
- (size.Width + v.LayoutParameters.Margins.TotalWidth())/2;
case Gravity.CenterHorizontal:
nfloat boxWidth = newPosition.Right - newPosition.Left -
Padding.TotalWidth() - v.LayoutParameters.Margins.TotalWidth();
nfloat offsetInBox = (boxWidth - size.Width)/2;
x = newPosition.Left + Padding.Left + v.LayoutParameters.Margins.Left +
offsetInBox;
break;
}

Expand Down Expand Up @@ -494,13 +506,13 @@ private nfloat getTotalSpacing()
// Helper to get the total measured height of all subviews, including all padding and margins
private nfloat getTotalMeasuredHeight()
{
return (nfloat)(Padding.TotalWidth() + getTotalSpacing() + SubViews.Where(x=>!x.Gone).Sum(x=>x.GetMeasuredSize().Height + x.LayoutParameters.Margins.TotalHeight()));
return (nfloat)(Padding.TotalHeight() + getTotalSpacing() + SubViews.Where(x=>!x.Gone).Sum(x=>x.GetMeasuredSize().Height + x.LayoutParameters.Margins.TotalHeight()));
}

// Helper to get the total measured width of all subviews, including all padding and margins
private nfloat getTotalMeasuredWidth()
{
return (nfloat)(Padding.TotalHeight() + getTotalSpacing() + SubViews.Where(x=>!x.Gone).Sum(x=>x.GetMeasuredSize().Width + x.LayoutParameters.Margins.TotalWidth()));
return (nfloat)(Padding.TotalWidth() + getTotalSpacing() + SubViews.Where(x=>!x.Gone).Sum(x=>x.GetMeasuredSize().Width + x.LayoutParameters.Margins.TotalWidth()));
}

// Helper to adjust the parent width passed down to subviews during measurement
Expand Down
85 changes: 84 additions & 1 deletion XibFree/PublicExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using System;
using UIKit;
using System.Collections.Generic;

namespace XibFree
{
Expand Down Expand Up @@ -47,5 +48,87 @@ public static View GetLayoutRoot(this UIView view)
var host = view.GetLayoutHost();
return host.Layout;
}

/// <summary>
/// Returns the top-level ViewGroup that this view is
/// a part of. Returns null if it is not under a group
/// yet. If view is the root, it is returned.
/// </summary>
public static ViewGroup FindRootGroup(this View view)
{
if (view.Parent == null && view is ViewGroup)
{
return (ViewGroup)view;
}

ViewGroup parent = null;
while (view.Parent != null)
{
view = parent = view.Parent;
}
return parent;
}

/// <summary>
/// Returns the top-level UIView that this view is being hosted under.
/// Returns null if not hosted.
/// </summary>
public static UIView FindRootUIView(this View view)
{
return FindRootGroup(view)?.GetHost()?.GetUIView();
}

/// <summary>
/// Returns the nearest parent ViewGroup that this view is
/// a part of. Returns null if it is not under a group
/// yet. Returns the view passed if it is actually a
/// ViewGroup.
/// </summary>
public static ViewGroup FindNearestGroup(this View view)
{
while (true)
{
if (view is ViewGroup)
{
return (ViewGroup)view;
}
view = view.Parent;
if (view == null)
{
return null;
}
}
}

/// <summary>
/// Returns the UIView associated with nearest parent ViewGroup
/// that this view is a part of. Returns null if it is not under
/// a group yet. Returns the UIView for the view passed if it is
/// actually a ViewGroup.
/// </summary>
public static UIView FindNearestGroupUIView(this View view)
{
return view.FindNearestGroup()?.GetHost()?.GetUIView();
}

/// <summary>
/// Returns all UIViews at or under the passed view.
/// </summary>
public static IEnumerable<UIView> FindUIViews(this View view)
{
var views = new List<UIView>();
if (view is NativeView)
{
views.Add((view as NativeView).View);
}
else if (view is ViewGroup)
{
foreach (var child in (view as ViewGroup).SubViews)
{
views.AddRange(FindUIViews(child));
}
}
return views;
}
}
}
}
16 changes: 12 additions & 4 deletions XibFree/UILayoutHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System;
using UIKit;
using CoreGraphics;
using System.Diagnostics;

namespace XibFree
{
Expand Down Expand Up @@ -99,10 +100,17 @@ public override void LayoutSubviews()
{
if (_layout!=null)
{
// Remeasure
_layout.Measure(Bounds.Width, Bounds.Height);
// Apply layout
_layout.Layout(Bounds, false);
try
{
// Remeasure
_layout.Measure(Bounds.Width, Bounds.Height);
// Apply layout
_layout.Layout(Bounds, false);
}
catch (Exception ex)
{
Trace.WriteLine("XibFree LayoutSubviews error: " + ex.Message);
}
}
}

Expand Down
73 changes: 71 additions & 2 deletions XibFree/View.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using UIKit;
using CoreGraphics;
using CoreAnimation;
using System.Diagnostics;

namespace XibFree
{
Expand Down Expand Up @@ -75,7 +76,7 @@ public virtual LayoutParameters LayoutParameters
}

// Internal helper to walk the parent view hierachy and find the view that's hosting this view hierarchy
internal virtual ViewGroup.IHost GetHost()
public virtual ViewGroup.IHost GetHost()
{
if (_parent==null)
return null;
Expand All @@ -100,7 +101,14 @@ internal virtual void onDetach()
/// <param name="newPosition">The new position of this view</param>
public void Layout(CGRect newPosition, bool parentHidden)
{
onLayout(newPosition, parentHidden);
try
{
onLayout(newPosition, parentHidden);
}
catch (Exception ex)
{
Trace.WriteLine("XibFree Layout error: " + ex.Message);
}
}

/// <summary>
Expand Down Expand Up @@ -224,7 +232,68 @@ public bool Visible
LayoutParameters.Visibility = value ? Visibility.Visible : Visibility.Invisible;
}
}

public void Hide(bool collapsed, bool animate)
{
if (LayoutParameters.Visibility != Visibility.Visible)
{
// Already hidden or in progress
// Console.WriteLine("Skipping Hide: already hidden");
return;
}
LayoutParameters.Visibility = (collapsed) ? Visibility.Gone : Visibility.Invisible;
ShowHide(false, animate);
}

public void Show(bool animate)
{
if (LayoutParameters.Visibility == Visibility.Visible)
{
// Already visible or in progress
// Console.WriteLine("Skipping Show: already shown");
return;
}
LayoutParameters.Visibility = Visibility.Visible;
ShowHide(true, animate);
}

public void SetVisibility(bool visible, bool animate)
{
if (visible)
Show(animate);
else
Hide(true, animate);
}

public void SetVisibility(Visibility visibility, bool animate)
{
if (LayoutParameters.Visibility != visibility)
{
LayoutParameters.Visibility = visibility;
ShowHide(visibility == Visibility.Visible, animate);
}
}

private void ShowHide(bool show, bool animate)
{
//Console.WriteLine("ShowHide(show=" + show + ",animate=" + animate + ")");
double delay = animate ? 0.25f : 0.0;
//double delay = 0;
var root = this.FindRootUIView();
UIView.AnimateNotify(delay, delegate
{
foreach (var uiview in this.FindUIViews())
{
uiview.Alpha = (show) ? 1.0f : 0.0f;
}
}, (bool completed) =>
{
//Console.WriteLine("completed=" + completed);
//root?.Superview?.SetNeedsLayout();
root?.SetNeedsLayout();
});
}

public void RemoveFromSuperview()
{
if (Parent!=null)
Expand Down
Loading