Search code examples
iosswiftuidatepicker

Why is there empty space between my UIDatePicker and its toolbar when using the new datepickerstyle?


My iOS app uses a lot of UIDatePickers as input views for UITextFields. Before iOS 14, they worked perfectly and didn't have any issues. With the new "compact" style, though, for some reason there is a bar of transparent space in between the datepicker and its toolbar. This only happens on the iPhone X and later, so I'm assuming it's something to do with the curved screen, but I don't see what part of the code is causing the issue. Below are a code snippet of the datepicker as well as a screenshot of the issue— any help would be appreciated.

            let toolbar = UIToolbar()
            toolbar.sizeToFit()
            let finishedButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(donedatePicker))
            finishedButton.tag = datePickerField!.tag
            let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
            let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelDatePicker))
            cancelButton.tag = datePickerField!.tag
            
            toolbar.setItems([cancelButton,spaceButton,finishedButton], animated: false)
            datePickerField!.inputAccessoryView = toolbar //datePickerField is the UITextField

The gray background and the "save changes" button are the actual app itself.

enter image description here


Solution

  • Apparently the new .compact style is meant to be used in place of the textfield, not as an inputview. So if you want to have it as an inputview like I do, you should just change the style back to .wheels.