Repository navigation
Expand file tree
/
Copy pathViewExtension.cs
More file actions
79 lines (69 loc) · 3.03 KB
/
Copy pathViewExtension.cs
File metadata and controls
79 lines (69 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System.Linq;
using UIKit;
namespace iNormal.FluentAutoLayout
{
public static class ViewExtension
{
/// <summary>
/// Add view to parent's sub view and return a parent constraint object that can use to chain method / make fluent method call.
/// </summary>
public static ParentConstraint SetParentAndConstraints(this UIView view, UIView parent)
{
// make sure this is off to allow auto layout work correctly
view.TranslatesAutoresizingMaskIntoConstraints = false;
// add the view as sub view first
if (parent != null)
{
if (parent is UIStackView parentStackView)
{
parentStackView.AddArrangedSubview(view);
}
else
{
parent.AddSubview(view);
}
}
// return a parent constraint object that can use to chain method / make fluent method call.
return new ParentConstraint(view, parent);
}
/// <summary>
/// Add view to table view's header and return a parent constraint object that can use to chain method / make fluent method call.
/// </summary>
public static ParentConstraint SetTableHeaderAndConstraints(this UIView view, UITableView parent)
{
// make sure this is off to allow auto layout work correctly
view.TranslatesAutoresizingMaskIntoConstraints = false;
// add the view as header view
if (parent != null)
{
parent.TableHeaderView = view;
}
// return a parent constraint object that can use to chain method / make fluent method call.
return new ParentConstraint(view, parent);
}
/// <summary>
/// Add view to table view's footer and return a parent constraint object that can use to chain method / make fluent method call.
/// </summary>
public static ParentConstraint SetTableFooterAndConstraints(this UIView view, UITableView parent)
{
// auto layout for table view footer is broken
// don't automatically disable TranslatesAutoresizingMaskIntoConstraints
// add the view as header view
if (parent != null)
{
parent.TableFooterView = view;
}
// return a parent constraint object that can use to chain method / make fluent method call.
return new ParentConstraint(view, parent);
}
/// <summary>
/// Retrieve the target constraint related with this view from it's parent.
/// </summary>
public static NSLayoutConstraint GetConstraint(this UIView view, NSLayoutAttribute attribute)
{
return view.Superview.Constraints.FirstOrDefault(x =>
x.FirstItem == view &&
x.FirstAttribute == attribute);
}
}
}