Reputation: 733
I want my date picker to show the text right aligned as it is contained in an right aligned tableView. I'm using the iOS 14 default style were selecting the date picker ends up in a popover to select the date, so by default the label is left aligned when the popover is not showing.
I tried using datePicker.contentHorizontalAlignment = .right
but nothing is changing.
Is there any other way to change the alignment as it is looking strange that way?
Upvotes: 8
Views: 5204
Reputation: 3177
There's no explicit way to do this, since the widget itself seems to not honor the content alignment; BUT: I have found a way to do this that works with iOS currently. At some point the date picker creates a UILabel, seemingly with a 'leading' constraint, no matter what content alignment. But, we can override what that 'leading' means with clever use of semanticContentAttribute
.
datePicker.semanticContentAttribute = .forceRightToLeft
datePicker.subviews.first?.semanticContentAttribute = .forceRightToLeft
It's possible only the second line of code is necessary, but my first attempt was with the first line, which didn't work, so I added the second line.
Upvotes: 15