Reputation: 583
Is there any way possible to set a UIDatePicker
in XCode-Interface Builder to just use the rollers of the "month" and "year" without the "day"? I've tried checking the file properties in the IB yet failed to find a solution for my problem...
Upvotes: 37
Views: 89821
Reputation: 119177
Apple has finally added the case .yearAndMonth
which is only supported in the UIDatePickerStyleWheels
:
let datePicker = UIDatePicker ()
datePicker.datePickerMode = .yearAndMonth
datePicker.preferredDatePickerStyle = .wheels
For older iOS versions, just go with datePicker.datePickerMode = .init(rawValue: 4269)
.
Upvotes: 7
Reputation: 6013
Simple Solution Swift 5+
if #available(iOS 17.4, *) {
datePicker.datePickerMode = .yearAndMonth
} else {
// Fallback on earlier versions
datePicker.datePickerMode = .date
datePicker.datePickerMode = .init(rawValue: 4269) ?? .date
}
datePicker.preferredDatePickerStyle = .wheels
Upvotes: 1
Reputation: 2717
See UIDatePickerModeYearAndMonth
(iOS 17.4+)
Example:
let datePicker = UIDatePicker()
datePicker.datePickerMode = .yearAndMonth
datePicker.preferredDatePickerStyle = .wheels
Upvotes: -1
Reputation: 44633
Your question mentions date
and year
so I it seemed like UIDatePickerModeDate
would suffice but as you are looking for month
and year
which is not an available option. I suggest you consider using a two component UIPickerView
object.
Original Answer
You can do this by changing the Mode
property under Date Picker
to Date
in the Attributes Inspector
in the right bar ( Cmd + Option + 4 ). You can also do this programmatically,
datePicker.datePickerMode = UIDatePickerModeDate
Upvotes: 52
Reputation: 19996
What you can do is restrict the range of dates, that can be used for input using the properties maximumDate and minimumDate
This has the added benefit of being automatically localized. i.e. the order of month and day is correct.
Upvotes: 0
Reputation: 1496
I don't think this is possible using the standard UIDatePicker. If you have a look at the documentation it makes it clear that only four modes are available:
You may have to create your own.
Upvotes: 7