-
Notifications
You must be signed in to change notification settings - Fork 3
/
FormViewController.cs
143 lines (117 loc) · 4.71 KB
/
FormViewController.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
using System.Linq;
using Cirrious.FluentLayouts.Touch;
using UIKit;
namespace AutoScroll
{
public class FormViewController : UIViewController
{
private UITapGestureRecognizer _gesture;
private AutoScrollHelper _autoScrollHelper;
private UIView _contentView;
public override void ViewDidLoad()
{
base.ViewDidLoad();
Title = "Auto Scroll Test";
View.BackgroundColor = UIColor.White;
EdgesForExtendedLayout = UIRectEdge.None;
// Create containers
_contentView = new UIView();
var scrollView = new UIScrollView {_contentView};
Add(scrollView);
// Create form elements
const int count = 7;
for (var i = 1; i <= count; i++)
{
_contentView.Add(new UITextField
{
Placeholder = $"Test {i}",
BorderStyle = UITextBorderStyle.RoundedRect,
Tag = i,
ReturnKeyType = i < count ? UIReturnKeyType.Send : UIReturnKeyType.Done
});
}
var loginButton = new UIButton(UIButtonType.System);
loginButton.SetTitle("Save", UIControlState.Normal);
loginButton.TouchUpInside += (sender, args) => Save();
_contentView.Add(loginButton);
// Auto layout
View.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
View.AddConstraints(scrollView.FullWidthOf(View));
View.AddConstraints(scrollView.FullHeightOf(View));
View.AddConstraints(
_contentView.WithSameWidth(View),
_contentView.WithSameHeight(View).SetPriority(UILayoutPriority.DefaultLow)
);
scrollView.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
scrollView.AddConstraints(_contentView.FullWidthOf(scrollView));
scrollView.AddConstraints(_contentView.FullHeightOf(scrollView));
var formConstraints = _contentView
.VerticalStackPanelConstraints(new Margins(20), _contentView.Subviews);
// very important to make scrolling work
var bottomViewConstraint = _contentView.Subviews.Last().AtBottomOf(_contentView).Minus(20);
_contentView.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
_contentView.AddConstraints(formConstraints);
_contentView.AddConstraints(bottomViewConstraint);
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
// Hide keyboard when user taps on the View background
_gesture = View.DismissKeyboardOnTap();
_autoScrollHelper = new AutoScrollHelper(this);
for (var i = 0; i < _contentView.Subviews.Count(); i++)
{
var textField = _contentView.Subviews[i] as UITextField;
if (textField != null)
{
textField.Tag = i;
textField.ShouldReturn = ShouldReturn;
}
}
}
public override void ViewWillDisappear(bool animated)
{
base.ViewWillDisappear(animated);
_gesture.Dispose();
_gesture = null;
_autoScrollHelper.Dispose();
_autoScrollHelper = null;
for (var i = 0; i < _contentView.Subviews.Count(); i++)
{
var textField = _contentView.Subviews[i] as UITextField;
if (textField != null)
textField.ShouldReturn = null;
}
}
private bool ShouldReturn(UITextField textField)
{
if (textField.ReturnKeyType == UIReturnKeyType.Done)
{
// we are done, hide the keyboard
View.EndEditing(true);
// nothing else to edit, why not just saving the form?
Save();
return false;
}
var nextTag = textField.Tag + 1;
UIResponder nextControl = _contentView.ViewWithTag(nextTag);
if (nextControl != null)
{
// set focus on the next control
nextControl.BecomeFirstResponder();
}
else
{
// Not found, hide keyboard.
View.EndEditing(true);
}
return false;
}
private void Save()
{
var alert = UIAlertController.Create("Success", "All good here", UIAlertControllerStyle.Alert);
alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
PresentViewController(alert, true, null);
}
}
}