10

I compiled my app with Xcode 13 and iOS 15, and I noticed the UIDatePicker is getting right aligned.

enter image description here

You can see that my storyboard is properly setup with constraints:

enter image description here

This UI element was working fine, even if I download the App Store version of my app on a device running iOS 15, it renders correctly. It's only compiling it with Xcode 13 that messes is up.

Is anyone else encountering that issue?

KBog
  • 3,800
  • 1
  • 25
  • 31

2 Answers2

17

Just a quick follow-up on this – I submitted this issue via Feedback Assistant and Apple told me this is expected behavior:

Engineering has provided the following information regarding this issue:

This is intentional and not a bug. We right-align the UIDatePicker pills for compact. Please know that if you want to change the alignment of the UIDatePicker controls on iOS 15, we now support UIControl.contentHorizontalAlignment to do just that.

So the proper solution is as follows:

datePicker.contentHorizontalAlignment = .left
Dharman
  • 30,962
  • 25
  • 85
  • 135
jwuki
  • 367
  • 2
  • 13
  • This also worked for me. However, you would think the control alignment setting in Xcode would do the same thing. It did not work for me without this line of code. iOS 17. – Tap Forms Jul 19 '23 at 15:00
4

The old behavior of the compact Date Picker (UIDatePicker) was aligned to left (with Xcode 12 + iOS 14), but now with Xcode 13 is aligned to right.

I ran into the same problem when using Xcode 13 and found a solution here.

    if #available(iOS 15.0, *) {
        // make date picker align to left in Xcode 13 with iOS 15
       datePicker.subviews.first?.semanticContentAttribute = .forceRightToLeft
    }
benck
  • 2,034
  • 1
  • 22
  • 31
  • 1
    Thanks, that seems to have solved it. I'm just a bit worried whether this is too fragile? – KBog Oct 06 '21 at 17:02
  • 3
    This solution technically works, but just as an FYI, it swaps the time and date fields (time appears to the left of the date, rather than on the right as it should). – jwuki Oct 07 '21 at 03:29
  • 1
    I’m using the date only picker, so I didn’t notice that. It seems the ultimate solution is to wait Apple to let us customize the alignment of the datepicker… – benck Oct 08 '21 at 03:39
  • 1
    I use date only, and this solution doesn't seem to work. Picker is left aligned, I'm trying to pin it to right. – Codetard Oct 11 '21 at 08:20
  • Ah good catch @jwuki, just realized that the time is on the left side of the date. Not ideal. Maybe living with the whole thing right aligned is better? haha – KBog Oct 12 '21 at 05:22