In portrait orientation,
this app keeps the top edge of the date picker
at the bottom edge of the status bar.
In landscape orientation,
the status bar is hidden
and the the top edge of the date picker is at the top edge of the big white
View
.
The controls looked better in iOS 6:
A
UIDatePicker
is a
view
that
looked like a solid, metallic Las Vegas slot machine in iOS 6.
Now it looks like a rotating virtual force field.
The clicking sound when you rotate it is not played on the iOS Simulator.
We usually initialize a view by passing a
frame
rectangle to
init
,
but a
UIDatePicker
already has a default size of 375 × 216 pairs of pixels on iPhone 6
(undocumented).
We could resize it with its
transform
property and
CGAffineTransformMakeScale
,
but don’t.
Everyone expects that the
UIDatePicker
will remain its natural size.
We should specify only its position.
AppDelegate
:
unchanged.ViewController
:
unchanged.View
:
added three properties
and the methods
init
,
addViewControllerConstraint
,
and
valueChanged
.
A subview must be
added
to its superview
before
the subview’s size and position can be
constrained.
UIDatePickerMode
s:
init
method of class
View
.
dateFormatter.dateStyle = NSDateFormatterStyle.FullStyle; dateFormatter.timeStyle = NSDateFormatterStyle.FullStyle;to
//so we don't have to write the day of the week //in the string in "December 31, 2014" dateFormatter.dateStyle = NSDateFormatterStyle.LongStyle; //so we don't have to write the time //in the string in "December 31, 2014" dateFormatter.timeStyle = NSDateFormatterStyle.NoStyle; let date: NSDate? = dateFormatter.dateFromString("December 31, 2014")!; if date != nil { datePicker.date = date!; } //The output in the label should show day of week and time. dateFormatter.dateStyle = NSDateFormatterStyle.FullStyle; dateFormatter.timeStyle = NSDateFormatterStyle.FullStyle;
Wednesday, December 31, 2014 at 12:00:00 AM Eastern Standard Time
minimumDate
and a
maximumDate
.
dateStyle
to
NSDateFormatterStyle.NoStyle
,
and observe that the date is no longer printed.
Similarly, change the
timeStyle
to
NSDateFormatterStyle.NoStyle
,
and observe that the time is no longer printed.
(If they’re both
NoStyle
,
the word “at” won’t be printed either.)
Knowing all of this,
display the date in one label and the time in another label.
valueChanged
method of class
View
,
change
label.text = dateFormatter.stringFromDate(datePicker.date);to
let dueDate: NSDate = NSDate(timeInterval: 60 * 60 * 24 * 280, sinceDate: datePicker.date); label.text = dateFormatter.stringFromDate(dueDate);
date
property of the date picker.
//In the valueChanged method of the view controller let dateComponents: NSDateComponents = datePicker.calendar.components(NSCalendarUnit.WeekdayCalendarUnit, fromDate: datePicker.date); let i: Int = dateComponents.weekday; //1 for Sunday, 2 for Monday, etc. if i == 1 { //Go to the day after the Sunday. let monday: NSDate = NSDate(timeInterval: 60 * 60 * 24, sinceDate: datePicker.date); datePicker.setDate(monday, animated: true); }
init
View
immediately after creating the picker.
dateFormatter.calendar = NSCalendar(calendarIdentifier: NSHebrewCalendar); datePicker.calendar = dateFormatter.calendar; print("datePicker.calendar.calendarIdentifier = \(datePicker.calendar.calendarIdentifier)");
datePicker.calendar.calendarIdentifier = hebrew