Change text alignment of DatePicker in SwiftUI 2.0 iOS 14
Here is possible solution. Tested with Xcode 12 / iOS 14
DatePicker(selection: $birthDate, in: ...Date().stripTime(), displayedComponents: .date) {
Text("Birthday").frame(maxWidth: .infinity, alignment: .trailing)
}.fixedSize().frame(maxWidth: .infinity, alignment: .trailing)
Update:
HStack {
Text("Birthday")
Spacer()
DatePicker("", selection: $birthDate, in: ...Date().stripTime(), displayedComponents: .date)
.fixedSize()
}
UIDatePicker rendering is getting right aligned in Xcode 13 (iOS 15)
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
Change the text alignment for Eureka TextRow
For Eureka is a little different. You have to use cell update callback. It wasn't well documented about changing alignment. But it did mention here that to change color or font you will have to use the .cellUpdate.
Thus, i believe this work will work for you:
let row = TextRow() {
row.title = "Field:"
}.cellUpdate { cell, row in
cell.textField.textAlignment = .left
}
Anyone find that UIDatePicker is broken under iOS 14?
To add some additional information to Oscar's answer...
UIDatePicker
now has three different styles, as per documentation here and here. The date picker's style is accessible by the .preferredDatePickerStyle
property which can be set to four different cases:
.automatic
- A style indicating that the system picks the concrete style based on the current platform and date picker mode..compact
- A style indicating that the date picker displays as a label that when tapped displays a calendar-style editor..inline
- A style indicating that the date pickers displays as an inline, editable field..wheels
- A style indicating that the date picker displays as a wheel picker.
If you want things to look how they did pre iOS 14, stick with the .wheels
style. It is also worth noting that not all styles can accommodate all date and time settings, hence why you can only set a preferred style.
Lastly, this property is only available in iOS 13.4 or newer, so you will have to accomplish version control with something like:
if #available(iOS 13.4, *) {
yourDatePicker.preferredDatePickerStyle = UIDatePickerStyle.automatic
} else {
// Fallback on earlier versions
}
Related Topics
Programmatically Place Partial Image Over Another in UIview Using Swift 3
Why Does Compactmap Return a Nil Result
Nsjsonserialization Error. Code=3840 "Invalid Value Around Character 0
Pfobject Unable to Be Cast to Custom Subclass
Where Do I Register a Valuetransformer in Swift
Facebook Graphrequest for Swift 5 and Facebook Sdk 5
Views Are Merged Instead of Showing Them Separately
Error Loading Media Sources in Mlmedialibrary
Getting Common Data from Two Different Types of Array
Custom Radix Columns (+Special Characters)
"Ambiguous Use" on Generic Method After Migration to Swift 4
Why Does Cabasicanimation Try to Initialize Another Instance of My Custom Calayer
Difference Between Firinstanceid.Instanceid().Token() and Messaging.Messaging().Fcmtoken
How to Solve This Error: Thread 1: Exc_Resource Resource_Type_Memory (Limit=650 Mb, Unused=0X0)
Swiftycam Capture Session Is Not Running
Inner Didset Protection Bizarrely Extends to The Whole Class