Position Date Picker

Hello, I’m obviously quite new to this. Is there a way I can better position this date picker? it functionally works, but it’s not very pretty.

thanks again



    @objc private func didTapAdd() {
        let getInfo = UIAlertController(
            title: "Event Name & Date",
            message: "",
            preferredStyle: .alert
            )
        //add field(s)
        
        getInfo.addTextField { field in
            field.placeholder = "Event Name"
            field.returnKeyType = .next
            field.autocapitalizationType = .words
            field.keyboardType = .default
        }
        
        let datePicker:UIDatePicker = UIDatePicker()
        
        getInfo.view.addSubview(datePicker)
        datePicker.datePickerMode = .date
        // .labelsHidden()
     
        
        datePicker.addTarget(self,
                             action: #selector(ViewController.userSelectedDate),
                             for: .valueChanged)
        
        //Add buttons
        getInfo.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
        getInfo.addAction(UIAlertAction(title: "Save", style: .default, handler:{ _ in
            
            guard let textFields = getInfo.textFields else { return }
            
            let eventName = textFields[0].text
         
            self.createItem(name: eventName!, eventDate: self.pickedDate)

        } ))
      
        present(getInfo, animated: true)
        
    }

Honestly, I’m not even sure UIAlertController actually supports adding additional controls like a UIDatePicker.

If you look at the docs for UIAlertController, it says:

The UIAlertController class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

I have bolded the part I think is the issue here. Calling addSubview(_:) is explicitly modifying the view hierarchy.

Outside of the approved messages, buttons and textfields, I think you’re out of luck. (And note that you don’t actually add a UIButton or a UITextField yourself; you call methods that handle the details of adding them for you based on the info you supply.)

Inserting another control, say between the message text and the buttons, would require applying constraints in order to position everything correctly, but you really can’t access the positions of the other elements in order to have something to anchor your constraints to. You could probably anchor the constraints directly to the UIAlertController’s view, but the default controls likely wouldn’t acknowledge them, which means you’d end up with weird overlapping controls and text. Maybe you could fix that by resizing the enclosing UIView, but then what else would break because you’re messing with a view hierarchy that Apple warns is private and shouldn’t be messed with?

I think the only way you’d be able to do something like this is to roll your own custom alert view.

I could always be wrong, though.

you are probably correct.

My only goal is to collect a text field (eventName) and a date (eventDate) (and eventually a single selection of
No, Weekly, Bi-Weekly, Monthly, Yearly) (eventRepeats)

I’m not tied to a specific method, I was just building off someone’s example app.
if it’s more appropriate to use another way, I’m more than open to giving it a try.

thanks

You could quickly make your own VC for a custom alert that wouldn’t be too much overhead only wanting those few things on it