Setting the Datepicker in IOS to pick only the Month and Year
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;
How to accept only Month and Year using DatePicker in iOS
Choice-1
Objective-C
yourDatepicker.datePickerMode = UIDatePickerModeDate;
Swift
yourDatepicker.datePickerMode = UIDatePickerMode.Date
Swift3
yourDatepicker.datePickerMode = .date
the another modes are
- UIDatePickerModeTime,
- UIDatePickerModeDate,
- UIDatePickerModeDateAndTime,
- UIDatePickerModeCountDownTimer
Choice-2
the above method not working well, then check this link
How do I create a DatePicker in swiftUI that only select years and I want this to be in prespecified range of years?
You can create your custom year picker by using a simple Picker
and passing it an array of Int
as your years.
struct YearPicker: View {
private let numberFormatter: NumberFormatter = {
let nf = NumberFormatter()
nf.usesGroupingSeparator = false
return nf
}()
@State private var selectedYearIndex: Int
private let years: [Int]
init(start: Int, end: Int) {
let years = Array(start...end)
self._selectedYearIndex = State(initialValue: years.startIndex)
self.years = years
}
func yearString(at index: Int) -> String {
let selectedYear = years[index]
return numberFormatter.string(for: selectedYear) ?? selectedYear.description
}
var body: some View {
VStack {
Text("Your birth year is \(yearString(at: selectedYearIndex))")
Picker("Year", selection: $selectedYearIndex) {
ForEach(years.indices) { yearIndex in
Text("\(self.yearString(at: yearIndex))")
}
}
}
}
}
struct YearPicker_Previews: PreviewProvider {
static var previews: some View {
YearPicker(start: 2000, end: 2020)
}
}
Setting only specific months in UIDatePicker Swift 5
You can try something like this:
datePicker.maximumDate = Calendar.current.date(byAdding: .month, value: 3, to: Date())
How to display only month and year in UIDatePicker?
You need to make your own custom class.As UIDatePicker not provide any api's to do that.You need to make custom picker and than fill with appropriate datasource.This code will help you and does same.
Setting the Datepicker in IOS to pick only the Day and month
UIDatePicker
doesn't support this. You will need to use UIPickerView
and add whatever components you need with whatever values should be shown.
To format the date in your text field, use an NSDateFormatter
to format the date as needed.
UIDatePicker select Month and Year
Yeah, you probably want to just make your own picker. You don't have to subclass it or anything, though; just use a generic UIPickerView
and return appropriate values from your UIPickerViewDelegate
/UIPickerViewDataSource
methods.
Related Topics
Easiest Way to Detect Internet Connection on Ios
Left Align Cells in Uicollectionview
Controlling the Screenshot in the iOS 7 Multitasking Switcher
Play Youtube Videos With Mpmovieplayercontroller Instead of Uiwebview
Ios: Convert Utc Nsdate to Local Timezone
How to Create a Multiline Uitextfield
Openurl Not Work in Action Extension
Prevent Screen Capture in an iOS App
How to Insert New Cell into Uitableview in Swift
Push Notification Issue With iOS 10
How to Detect Avplayer and Get Url of Current Video from Wkwebview
How to Pass Textfield Value to View Controller Through Button Click in Swift Ui
Iphone Hide Navigation Bar Only on First Page
Converting Storyboard from Iphone to Ipad