This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathPickerViewController.xib.cs
247 lines (213 loc) · 6.49 KB
/
PickerViewController.xib.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//
// The PickerViewController
//
using Foundation;
using UIKit;
using CoreGraphics;
using System;
namespace MonoCatalog {
public partial class PickerViewController : UIViewController {
UIPickerView myPickerView, customPickerView;
UIDatePicker datePickerView;
UILabel label;
UIColor backgroundColor;
UIColor labelTextColor;
bool greaterThanSeven;
public PickerViewController () : base ("PickerViewController", null)
{
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
Title = "Picker";
greaterThanSeven = UIDevice.CurrentDevice.CheckSystemVersion (7, 0);
backgroundColor = greaterThanSeven ? UIColor.White : UIColor.Clear;
labelTextColor = greaterThanSeven ? UIColor.Black : UIColor.White;
NavigationController.NavigationBar.Translucent = false;
NavigationController.NavigationBar.BackgroundColor = backgroundColor;
View.BackgroundColor = greaterThanSeven ? UIColor.White : UIColor.Black;
CreatePicker ();
CreateDatePicker ();
CreateCustomPicker ();
// Colors
buttonBarSegmentedControl.TintColor = UIColor.DarkGray;
pickerStyleSegmentedControl.TintColor = UIColor.DarkGray;
label = new UILabel (new CGRect (20f, myPickerView.Frame.Y - 30f, View.Bounds.Width - 40f, 30f)) {
Font = UIFont.SystemFontOfSize (14),
TextAlignment = UITextAlignment.Center,
TextColor = labelTextColor,
BackgroundColor = UIColor.Clear
};
View.AddSubview (label);
buttonBarSegmentedControl.SelectedSegment = 0;
datePickerView.Mode = UIDatePickerMode.Date;
}
public override void ViewWillAppear (bool animated)
{
NavigationController.NavigationBar.BarStyle = greaterThanSeven ? UIBarStyle.Default : UIBarStyle.Black;
UIApplication.SharedApplication.StatusBarStyle = greaterThanSeven ? UIStatusBarStyle.Default : UIStatusBarStyle.BlackOpaque;
TogglePickers (buttonBarSegmentedControl);
}
public override void ViewWillDisappear (bool animated)
{
if (currentPicker != null)
currentPicker.Hidden = true;
NavigationController.NavigationBar.BarStyle = UIBarStyle.Default;
UIApplication.SharedApplication.StatusBarStyle = UIStatusBarStyle.Default;
}
CGRect PickerFrameWithSize (CGSize size)
{
var screenRect = UIScreen.MainScreen.ApplicationFrame;
return new CGRect (0f, screenRect.Height - 84f - size.Height, size.Width, size.Height);
}
UIView currentPicker;
void ShowPicker (UIView picker)
{
if (currentPicker != null) {
currentPicker.Hidden = true;
label.Text = "";
}
picker.Hidden = false;
currentPicker = picker;
}
#region Hooks from Interface Builder
[Export ("togglePickers:")]
public void TogglePickers (UISegmentedControl sender)
{
switch (sender.SelectedSegment) {
case 0:
pickerStyleSegmentedControl.Hidden = true;
segmentLabel.Hidden = true;
ShowPicker (myPickerView);
break;
case 1:
pickerStyleSegmentedControl.SelectedSegment = 1;
datePickerView.Mode = UIDatePickerMode.Date;
pickerStyleSegmentedControl.Hidden = false;
segmentLabel.Hidden = false;
ShowPicker (datePickerView);
break;
case 2:
pickerStyleSegmentedControl.Hidden = true;
segmentLabel.Hidden = true;
ShowPicker (customPickerView);
break;
}
}
[Export ("togglePickerStyle:")]
public void TogglePickerStyle (UISegmentedControl sender)
{
switch (sender.SelectedSegment) {
case 0: // time
datePickerView.Mode = UIDatePickerMode.Time;
break;
case 1: // date
datePickerView.Mode = UIDatePickerMode.Date;
break;
case 2: // date & time
datePickerView.Mode = UIDatePickerMode.DateAndTime;
break;
case 3: // counter
datePickerView.Mode = UIDatePickerMode.CountDownTimer;
break;
}
datePickerView.Date = NSDate.Now; //DateTime.Now;
Console.WriteLine ("Date is: {0} {1} {2}", NSDate.Now.ToString (), ((NSDate) DateTime.Now).ToString (), DateTime.Now);
}
#endregion
#region Custom picker
public void CreateCustomPicker ()
{
customPickerView = new UIPickerView (CGRect.Empty) {
AutoresizingMask = UIViewAutoresizing.FlexibleWidth,
Model = new CustomPickerModel (),
ShowSelectionIndicator = true,
BackgroundColor = backgroundColor,
Hidden = true
};
customPickerView.Frame = PickerFrameWithSize (customPickerView.SizeThatFits (CGSize.Empty));
View.AddSubview (customPickerView);
}
#endregion
#region Date picker
public void CreateDatePicker ()
{
datePickerView = new UIDatePicker (CGRect.Empty) {
AutoresizingMask = UIViewAutoresizing.FlexibleWidth,
Mode = UIDatePickerMode.Date,
BackgroundColor = backgroundColor,
Hidden = true
};
datePickerView.Frame = PickerFrameWithSize (datePickerView.SizeThatFits (CGSize.Empty));
View.AddSubview (datePickerView);
}
#endregion
#region People picker
void CreatePicker ()
{
//
// Empty is used, since UIPickerViews have auto-sizing,
// all that is required is the origin
//
myPickerView = new UIPickerView (CGRect.Empty) {
AutoresizingMask = UIViewAutoresizing.FlexibleWidth,
ShowSelectionIndicator = true,
Model = new PeopleModel (this),
BackgroundColor = backgroundColor,
Hidden = true
};
// Now update it:
myPickerView.Frame = PickerFrameWithSize (myPickerView.SizeThatFits (CGSize.Empty));
View.AddSubview (myPickerView);
}
public class PeopleModel : UIPickerViewModel {
static string [] names = new string [] {
"Brian Kernighan",
"Dennis Ritchie",
"Ken Thompson",
"Kirk McKusick",
"Rob Pike",
"Dave Presotto",
"Steve Johnson"
};
PickerViewController pvc;
public PeopleModel (PickerViewController pvc)
{
this.pvc = pvc;
}
public override nint GetComponentCount (UIPickerView v)
{
return 2;
}
public override nint GetRowsInComponent (UIPickerView pickerView, nint component)
{
return names.Length;
}
public override string GetTitle (UIPickerView picker, nint row, nint component)
{
if (component == 0)
return names [row];
else
return row.ToString ();
}
public override void Selected (UIPickerView picker, nint row, nint component)
{
pvc.label.Text = String.Format ("{0} - {1}",
names [picker.SelectedRowInComponent (0)],
picker.SelectedRowInComponent (1));
}
public override nfloat GetComponentWidth (UIPickerView picker, nint component)
{
if (component == 0)
return 240f;
else
return 40f;
}
public override nfloat GetRowHeight (UIPickerView picker, nint component)
{
return 40f;
}
}
#endregion
}
}