Post

Replies

Boosts

Views

Activity

UIDatePicker input view misaligns
I inherited an iOS project written in Swift 4.2, that uses a UIDatePicker as the input view of a text field: let datePicker = UIDatePicker() datePicker.maximumDate = Date() datePicker.datePickerMode = .date datePicker.date = self.someDate someTextField.inputView = datePicker datePicker.addTarget(self, action: #selector(handleDatePicker(sender:)), for: .valueChanged) ^ The code snippet above is slightly obfuscated, but shows the basic logic. The resulting input view is bottom-left aligned to the screen, with the majority of its bottom off-screen. (Sorry I can only describe it because I can't find a image host whose links are allowed on these forums.) I've tried to manually align the input view by setting auto layout constraints: let datePicker = UIDatePicker() datePicker.maximumDate = Date() datePicker.datePickerMode = .date datePicker.date = self.someDate datePicker.translatesAutoresizingMaskIntoConstraints = false datePicker.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true datePicker.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true datePicker.widthAnchor.constraint(equalTo: self.view.widthAnchor).isActive = true someTextField.inputView = datePicker datePicker.addTarget(self, action: #selector(handleDatePicker(sender:)), for: .valueChanged) However, this only results in the program crashing. What should I do to make the input view align properly on the screen?
0
0
423
Dec ’20