-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNestedStacksViewController.cs
149 lines (121 loc) · 5.66 KB
/
NestedStacksViewController.cs
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using UIKit;
using UIStackViewPlayground.Helpers;
namespace UIStackViewPlayground.Views
{
public class NestedStacksViewController : BaseViewController
{
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Main scrolled stack
var stackView = new UIStackView
{
TranslatesAutoresizingMaskIntoConstraints = false,
Axis = UILayoutConstraintAxis.Vertical,
Alignment = UIStackViewAlignment.Fill,
Distribution = UIStackViewDistribution.EqualSpacing,
Spacing = 4,
LayoutMargins = new UIEdgeInsets(4, 4, 4, 4), // apply padding
LayoutMarginsRelativeArrangement = true // apply padding
};
var scroll = new UIScrollView { TranslatesAutoresizingMaskIntoConstraints = false };
Add(scroll);
scroll.AddSubview(stackView);
scroll.FullSizeOf(View);
stackView.FullSizeOf(scroll);
stackView.WidthAnchor.ConstraintEqualTo(scroll.WidthAnchor).Active = true;
// Nested stacks
var nested1 = new UIStackView
{
TranslatesAutoresizingMaskIntoConstraints = false,
Axis = UILayoutConstraintAxis.Horizontal,
Alignment = UIStackViewAlignment.Fill,
Distribution = UIStackViewDistribution.FillEqually,
Spacing = 4
};
var nested2 = new UIStackView
{
TranslatesAutoresizingMaskIntoConstraints = false,
Axis = UILayoutConstraintAxis.Horizontal,
Alignment = UIStackViewAlignment.Fill,
Distribution = UIStackViewDistribution.EqualSpacing,
Spacing = 4
};
var nested3 = new UIStackView
{
TranslatesAutoresizingMaskIntoConstraints = false,
Axis = UILayoutConstraintAxis.Horizontal,
Alignment = UIStackViewAlignment.Fill,
Distribution = UIStackViewDistribution.FillProportionally,
Spacing = 4
};
var nested4 = new UIStackView
{
TranslatesAutoresizingMaskIntoConstraints = false,
Axis = UILayoutConstraintAxis.Horizontal,
Alignment = UIStackViewAlignment.Top,
Distribution = UIStackViewDistribution.EqualCentering,
Spacing = 4
};
var nested5 = new UIStackView
{
TranslatesAutoresizingMaskIntoConstraints = false,
Axis = UILayoutConstraintAxis.Horizontal,
Alignment = UIStackViewAlignment.Fill,
Distribution = UIStackViewDistribution.EqualSpacing,
Spacing = 4
};
for (var i = 0; i < 3; i++)
{
nested1.AddArrangedSubview(GetRandomView());
nested2.AddArrangedSubview(GetRandomView());
nested3.AddArrangedSubview(GetRandomView());
nested4.AddArrangedSubview(GetRandomView(50, 15 * (i + 1)));
}
stackView.AddArrangedSubview(GetNestedStackContainer("Fill Equally", nested1));
stackView.AddArrangedSubview(GetNestedStackContainer("Equal Spacing", nested2));
stackView.AddArrangedSubview(GetNestedStackContainer("Fill Proportionally", nested3));
for(var i = 0; i < 30; i++)
nested5.AddArrangedSubview(GetRandomView());
var nestedScroll = new UIScrollView { TranslatesAutoresizingMaskIntoConstraints = false };
nestedScroll.AddSubview(nested5);
stackView.AddArrangedSubview(GetNestedStackContainer("Nested stack with scroll", nestedScroll));
nested5.FullSizeOf(nestedScroll);
nested5.HeightAnchor.ConstraintEqualTo(nestedScroll.HeightAnchor).Active = true;
stackView.AddArrangedSubview(GetNestedStackContainer("Align Top", nested4));
}
private static UIView GetNestedStackContainer(string title, UIView stackView)
{
var view = new RoundedView {BackgroundColor = UIColor.LightGray};
view.Layer.BorderColor = UIColor.Gray.CGColor;
view.HeightAnchor.ConstraintEqualTo(110).Active = true;
var label = new UILabel
{
Text = title,
TranslatesAutoresizingMaskIntoConstraints = false
};
view.Add(label);
view.Add(stackView);
NSLayoutConstraint.ActivateConstraints(new []
{
label.LeadingAnchor.ConstraintEqualTo(view.LeadingAnchor, 10),
label.TrailingAnchor.ConstraintEqualTo(view.TrailingAnchor, -10),
label.TopAnchor.ConstraintEqualTo(view.TopAnchor, 8),
label.HeightAnchor.ConstraintEqualTo(22),
stackView.LeadingAnchor.ConstraintEqualTo(label.LeadingAnchor),
stackView.TrailingAnchor.ConstraintEqualTo(label.TrailingAnchor),
stackView.TopAnchor.ConstraintEqualTo(label.BottomAnchor, 5),
stackView.BottomAnchor.ConstraintEqualTo(view.BottomAnchor, -8)
});
return view;
}
private static UIView GetRandomView(float width = 50, float? height = null)
{
var view = new RoundedView { BackgroundColor = ColorUtil.GetRandomColor() };
view.WidthAnchor.ConstraintEqualTo(width).Active = true;
if (height != null)
view.HeightAnchor.ConstraintEqualTo(height.Value).Active = true;
return view;
}
}
}