Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open UIDatePicker programmatically in iOS 14

I have this normal UIDatePicker (sorry that it's not in English, please ignore that):

my date picker

But the problem is that it only appears when I'm pressing the Date Picker:

image

And I wanted to know how to show the Date Picker like in the first image (that it's covering all the screen with the blur effect) only with code, so the user won't need to press the Date Picker to show the date selector. Thanks.

like image 804
אורי orihpt Avatar asked Aug 09 '20 21:08

אורי orihpt


2 Answers

You can use a datePicker with .compact style and put a UILabel on top of it and give it a background color to hide the datePicker behind it, and make sure the label user interaction is disabled then you should be able to tap on the datePicker and it will show the modal and then the value changed of the datePicker you can use your own date formatter to set the text of the label accordingly, you might also both the datePicker and the label inside a UIView and make it clip to bounds

like image 81
Amr Mohamed Avatar answered Sep 27 '22 00:09

Amr Mohamed


I have the same problem. Im using following hack:

let datePicker = UIDatePicker(frame: CGRect(x: 0, y: 0, width: 50, height: 50)) datePicker.preferredDatePickerStyle = .compact  // Removes Labels datePicker.subviews.forEach({ $0.subviews.forEach({ $0.removeFromSuperview() }) }) 

Then I simply add this date picker on top of a view, which should open this popup.

like image 20
A. Berisha Avatar answered Sep 27 '22 00:09

A. Berisha