Open Uidatepicker Programmatically in iOS 14

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:

  1. .automatic - A style indicating that the system picks the concrete style based on the current platform and date picker mode.
  2. .compact - A style indicating that the date picker displays as a label that when tapped displays a calendar-style editor.
  3. .inline - A style indicating that the date pickers displays as an inline, editable field.
  4. .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.
Sample Image

2.before click

Sample Image

3.click

Sample Image

4.after value changed

Sample Image

5.code behind

Sample Image

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



Leave a reply



Submit