Anyone find that UIDatePicker is broken under iOS 14?
To add some additional information to Oscar's answer...
UIDatePicker
now has three different styles, as per documentation here and here. The date picker's style is accessible by the .preferredDatePickerStyle
property which can be set to four different cases:
.automatic
- A style indicating that the system picks the concrete style based on the current platform and date picker mode..compact
- A style indicating that the date picker displays as a label that when tapped displays a calendar-style editor..inline
- A style indicating that the date pickers displays as an inline, editable field..wheels
- A style indicating that the date picker displays as a wheel picker.
If you want things to look how they did pre iOS 14, stick with the .wheels
style. It is also worth noting that not all styles can accommodate all date and time settings, hence why you can only set a preferred style.
Lastly, this property is only available in iOS 13.4 or newer, so you will have to accomplish version control with something like:
if #available(iOS 13.4, *) {
yourDatePicker.preferredDatePickerStyle = UIDatePickerStyle.automatic
} else {
// Fallback on earlier versions
}
How to make a UIDatePicker label invisible still interactive in Xamarin.ios?
To your first problem:
You can set the datepicker's PerferredDatePickerStyle as compact, and set the color of label which on the datepicker as white to cover the datepicker. Also set label's
enabled as false.
code like:
mydatapicker.PerferredDatePickerStyle=UIDatePickerStyle.Compact;
mylabel.BackgroundColor=UIColor.White;
//.....
To change it Text, add follow code:
mydatapicker.valuechanged+=Datepicker_Valuechanged;
private void Datepicker_Valuechanged(object sender,EventArgs e)
{mylabel.Text=mydatapicker.Date.ToString();}
To your second problem
I am afraid there is no appropriate way to close the calendar modal when any date is tapped.
Hope these could be helpful to you.
1.In storyboard,set label's enabled as false.
2.before click
3.click
4.after value changed
5.code behind
Conversion from iOS 13 UIDatepicker to iOS 14 UIDatepicker
just add this code in you viewDidLoad
or wherever you setup your datePicker:
if #available(iOS 13.4, *) {
yourDatePicker.preferredDatePickerStyle = .wheels
//yourDatePicker can be in code or IBOutlet
}
according to the documentation these are the styles available (iOS 13.4):
Styles
case automatic
A style indicating that the system picks the concrete style based on the current platform and date picker mode.case compact
A style indicating that the date picker displays as a label that when tapped displays a calendar-style editor.case inline
A style indicating that the date pickers displays as an inline, editable field.case wheels
A style indicating that the date picker displays as a wheel picker.
SwiftUI UIDatePicker .compact doesn't go directly to pop-up
With the Compact DatePicker you can't automatically show the calendar without selecting it from the part that show up at the bottom.
Open UIDatePicker programmatically in iOS 14
You can (if the 5 minute interval isn't a requirement) use GraphicalDatePickerStyle
which shows a very similar calendar.
struct ParentDatePicker: View {
@State var showGraphical: Bool = false
@State var currentDate: Date = Date()
var dateFormatter: DateFormatter {
let formatter = DateFormatter()
formatter.dateFormat = "dd MMM yyyy HH:mm"
return formatter
}
var body: some View {
Button(action: {
showGraphical.toggle()
}, label: {
Label(
title: { Text("\(currentDate, formatter: dateFormatter)") },
icon: { Image(systemName: "clock") })
})
.padding()
.frame(width: 200, height: 100)
.background(Color.white)
.cornerRadius(8)
.shadow(color: Color.black.opacity(0.1), radius: 30, x: 0 , y: 15)
.sheet(isPresented: $showGraphical, content: {
CusDatePicker(currentDate: $currentDate)
})
}
}
struct CusDatePicker: View {
@Binding var currentDate: Date
var body: some View {
//https://developer.apple.com/documentation/swiftui/datepicker
DatePicker("Date", selection: $currentDate, displayedComponents: [.date, .hourAndMinute])
//.datePickerStyle(CompactDatePickerStyle())
.datePickerStyle(GraphicalDatePickerStyle())
}
}
struct CusDatePicker_Previews: PreviewProvider {
static var previews: some View {
ParentDatePicker()
}
}
Related Topics
How to Convert Hex Number to Bin in Swift
Change the Value That Is Being Set in Variable's Willset Block
How to Stop Timer in Text View
Access Input from Uialertcontroller
Filter Array of [Anyobject] in Swift
Fibonacci Numbers Generator in Swift 3
Distinction in Swift Between Uppercase "Self" and Lowercase "Self"
How to Reason When I Have to Choose Between a Class, Struct and Enum in Swift
Objective-C Bridging Header for Frameworks
Remove Element from Collection During Iteration with Foreach
Hide Navigation Bar Without Losing Swipe Back Gesture in Swiftui
How to Include .Swift File from Other .Swift File in an Immediate Mode
How to Use Array.Filter to Filter a Class Object Based on a Property