iOS UIDatePicker display without time - ios

I would like to have a Date Picker with only one wheel, filled with days as on the Date and Time mode.
If I choose the Date mode, I have 3 wheels and I loose the name of the day.
In fact I would like something like the date and time mode, but only with the date.
I could use a UIPickerView, but I would have to fill it by myself with a very big array of data, as I want to be able go back far in time. With the Date picker, it's filled automatically, which is cool.
Is there a solution ?

You can also use UIDatePicker's pickerMode property to do that.
datePicker.pickerMode = .Date
Look into header file, you will see that there are few other Options that you can play with. Other values are;
Time
Date
DateAndTime
CountDownTimer

You should use the UIPickerView instead of the UIDatePicker. There is no possibility to customise UIDatePicker in the way you want.

try displayedComponents with .date
DatePicker( selection: .constant(futureDate), displayedComponents: [.date],
label: { Text("Due date") })

As you only need to show the contents that you have marked in the green box. You can use this https://github.com/attias/AADatePicker and modify it a bit and you can get it as per your requirement.
The changes you will need to make are within the AADatePickerView Class
1) In viewForRow method comment the following things
2) Second change

datePicker.datePickerMode = UIDatePicker.Mode.date
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd MMMM yyyy"
let selectedDate = dateFormatter.string(from: datePicker.date)
print(selectedDate)

Related

Accessibility Voice One for Date in a String

I have below code.
let text = "as of 07/29/2020"
textLabel.text = text
Voice Over read it - "as of 07 slash 29 slash 2 Thousand 20"
How can i make it to pronounce like "as of July, 29 2 Thousand 20"
Answer
Not sure exactly what you are trying to solve. But hopefully this helps.
Step 1: You need to use the accessibility label property label.accessibilityLabel = "some string"
Step 2: Convert your date into a more friendly string for voice over. You can do this by using a Date() object and format it differently for the label and the voice-over label. Here is a link for creating dates in swift.
Below is the link to the documentation and the section that mentions accessibility labels.
Playground Example: Your code will then look something like this.
import UIKit
import PlaygroundSupport
let label = UILabel()
let dateForLabel = formatDate(date: Date(), style: .short)
let dateStringForLabel = "as of \(dateForLabel)"
label.text = dateStringForLabel
let dateForVoiceOverLabel = formatDate(date: Date(), style: .long)
let dateStringForVoiceOver = "as of \(dateForVoiceOverLabel)"
label.accessibilityLabel = dateStringForVoiceOver
func formatDate(date: Date, style: DateFormatter.Style) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = style
dateFormatter.timeStyle = .none
let date = date
// US English Locale (en_US)
dateFormatter.locale = Locale(identifier: "en_US")
return dateFormatter.string(from: date) // Jan 2, 2001
}
Link to create a Date() object
How do you create a Swift Date object?
Link to apple documentation on Accessibility labels
https://developer.apple.com/documentation/uikit/accessibility_for_ios_and_tvos/supporting_voiceover_in_your_app
"Update Your App’s Accessibility
For elements that were not accessible to VoiceOver, start by improving their accessibility labels and hints:
The accessibilityLabel property provides descriptive text that VoiceOver reads when the user selects an element.
The accessibilityHint property provides additional context (or actions) for the selected element.
Accessibility labels are very important, because they provide the text that VoiceOver reads. A good accessibility label should be short and informative. It is important to note that a UILabel and an accessibilityLabel are different things. By default, VoiceOver reads the text associated with standard UIKit controls such as UILabel and UIButton. However these controls can also have corresponding accessibilityLabel properties to add more detail about the label or button..
Depending on the context, hints aren’t always necessary. In some cases, the label provides enough context. If you feel like you’re saying too much in an accessibility label, consider moving that text into a hint.
To ensure that users understand the intent of your interface, you might need to set some accessibility labels manually. Accessibility labels and hints can either be set in Xcode’s Identity Inspector or programmatically."

Datepicker is returning wrong date when set maximumDate

I have a datepicker where I have set maximumDate as 2 years before
Today is 15/07/2019 so 2 years before it becomes 15/07/2017
Now when I open datepicker, automatically picker goes from today date (15/07/2019) to 15/07/2017 because I have set maximumDate as 2 years before
Now I don't scroll datepicker and click Done button where I read the picker date.
print("mDoneAction===\(mDatePicker.date)")
This print the date as below.
2019-07-15 12:26:17 +0000
Any idea why its giving me 2019 instead of 2017 as picker is already set on 15-07-2017 and not 15-07-2019.
From docs
open var date: Date // default is current date when picker created.
so setting maximumDate doesn't affect date , you need to set date also
let d = UIDatePicker()
let year2Before = Calendar.current.date(byAdding: .year, value:-2, to: Date())!
d.date = year2Before
d.maximumDate = year2Before

How to advance other fields in datePicker automatically?

Let's say that I have a UIDatePicker and the mode is set to .date.
I want the following behaviour:
For example, March has 30 days. If I scroll below the 30th day, I want the month to be automatically incremented to April and so on.
Is there a property that does just that? I couldn't find one.
Thanks.
I don't think there's any way to do what you want with a normal UIDatePicker. You could attach an action to the valueChanged event, but that doesn't get called until the date picker stops moving.
You could probably create a custom date picker using a standard UIPickerView. You could use the data source methods to figure out when the "wheels" are turned, call selectedRow(inComponent:) to figure out which values are selected for each wheel, and then call selectRow(_:inComponent:animated) to switch the month if the user scrolls the day past the end of the month.
How to advance other fields in datePicker automatically?
The best way to do this is using the Target-Action design pattern. Basically, when a user chooses a new date (action), the view (a target) does something.
Here's some code that changes the day value to 1 if it's set to 0:
override func viewDidLoad() {
...
let datePicker = UIDatePicker()
datePicker.datePickerMode = .date
datePicker.addTarget(self, action: #selector(doStuff(sender:)), for: UIControlEvents.valueChanged)
view.addSubview(datePicker)
...
}
#IBAction func doStuff(sender: UIDatePicker) {
let calendar = Calendar.current
let components = calendar.dateComponents([.day], from: sender.date)
if components.day == 0 {
// Using Calendar.current automatically takes care of Time Zones for us.
if let date = calendar.date(byAdding: .day, value: 1, to: sender.date) {
sender.setDate(date, animated: true)
}
}
}
Note: This example is in Swift 4.
No, there is no property that does this that I am aware of.

iOS DatePicker not setting initial value

I am using storyboards and I have an outlet for the datePicker and I set the date like this in code
let date = NSDate().dateByAddingTimeInterval(-48 * 3600) //meaning 2 days ago the date
datePicker.setDate(date, animated: false)
But when I run the app it always shows the current date
After changing the "Date" property on the storyboard to "Custom" I could set the date on code as I showed above

UIView automatically reset to old frame after setting value from DatePicker to UITextField

Facing strange issue in swift. I making one demo that contain registration form and create on UIView in storyBoard and when user select Date TextField I am open this view using animation using following code:
UIView .animateWithDuration(0.3, animations: { () -> Void in
self.viewDate.frame = CGRectMake(0, UIScreen .mainScreen().bounds.size.height - self.viewDate.frame.size.height, self.viewDate.frame.size.width, self.viewDate.frame.size.height)
})
After change value in DatePicker that following method called:
#IBAction func select_BirthDate(sender:AnyObject) {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
let strDate = dateFormatter.stringFromDate(myDatePicker.date)
self.txtBirthDate.text = strDate
print("Date is : \(strDate)")
}
But Strange issue is that when select_BirthDate called and set its date in UITextField that appear view automatically disappear I didn't code for change it's frame and if i am comment self.txtBirthDate.text = strDate all things working parfect. even if I set value in UITextView then also working fine. issue only with UITextFiled I did not use any Layout for appear UIview.
HERE IT IS DEMO PROJECT LINK

Resources