带有 UIViewRepresentable 的 UIPickerView 不填满整个宽度
UIPickerView with UIViewRepresentable does not fill the whole width
不幸的是,当我在 SwiftUI 中使用 UIViewRepresenable 创建 UIPickerView 时,它并没有填满整个屏幕。我也试过给它很多框架修改器,但没有任何效果。有没有人对此有解决方案或者知道为什么整个屏幕的宽度没有被选择器填满?最好的问候
这是代码:
struct MultiComponentsPicker: UIViewRepresentable {
@Binding var hours: Int
@Binding var minutes: Int
@Binding var seconds: Int
func makeUIView(context: Context) -> UIPickerView {
let pickerView = UIPickerView()
pickerView.delegate = context.coordinator
pickerView.dataSource = context.coordinator
return pickerView
}
func updateUIView(_ uiView: UIPickerView, context: Context) {
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, UIPickerViewDataSource, UIPickerViewDelegate {
let parent: MultiComponentsPicker
init(_ parent: MultiComponentsPicker) {
self.parent = parent
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 6
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if component == 0 {
return 24
}
else if component == 1 {
return 1
}
else if component == 2 {
return 60
}
else if component == 3 {
return 1
}
else if component == 4 {
return 60
}
else if component == 5 {
return 1
}
return 0
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if component == 0 {
return "\(row)"
}
else if component == 1 {
return "Std."
}
else if component == 2 {
return "\(row)"
}
else if component == 3 {
return "Min."
}
else if component == 4 {
return "\(row)"
}
else if component == 5 {
return "Sek."
}
return ""
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if component == 0 {
parent.hours = Int(row.description)!
}
else if component == 2 {
parent.minutes = Int(row.description)!
}
else if component == 4 {
parent.seconds = Int(row.description)!
}
}
}
}
这就是它的样子:
在 UIViewRepresentable 中:
// allows rows to be compressed
pickerView.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
// allows rows to be expanded
pickerView.setContentHuggingPriority(.defaultLow, for: .horizontal)
在视图中:
MultiComponentsPicker(hours: $hours, minutes: $minutes, seconds: $seconds)
.frame(width: .infinity) // or fixed size
不幸的是,当我在 SwiftUI 中使用 UIViewRepresenable 创建 UIPickerView 时,它并没有填满整个屏幕。我也试过给它很多框架修改器,但没有任何效果。有没有人对此有解决方案或者知道为什么整个屏幕的宽度没有被选择器填满?最好的问候
这是代码:
struct MultiComponentsPicker: UIViewRepresentable {
@Binding var hours: Int
@Binding var minutes: Int
@Binding var seconds: Int
func makeUIView(context: Context) -> UIPickerView {
let pickerView = UIPickerView()
pickerView.delegate = context.coordinator
pickerView.dataSource = context.coordinator
return pickerView
}
func updateUIView(_ uiView: UIPickerView, context: Context) {
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, UIPickerViewDataSource, UIPickerViewDelegate {
let parent: MultiComponentsPicker
init(_ parent: MultiComponentsPicker) {
self.parent = parent
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 6
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if component == 0 {
return 24
}
else if component == 1 {
return 1
}
else if component == 2 {
return 60
}
else if component == 3 {
return 1
}
else if component == 4 {
return 60
}
else if component == 5 {
return 1
}
return 0
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if component == 0 {
return "\(row)"
}
else if component == 1 {
return "Std."
}
else if component == 2 {
return "\(row)"
}
else if component == 3 {
return "Min."
}
else if component == 4 {
return "\(row)"
}
else if component == 5 {
return "Sek."
}
return ""
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if component == 0 {
parent.hours = Int(row.description)!
}
else if component == 2 {
parent.minutes = Int(row.description)!
}
else if component == 4 {
parent.seconds = Int(row.description)!
}
}
}
}
这就是它的样子:
在 UIViewRepresentable 中:
// allows rows to be compressed
pickerView.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
// allows rows to be expanded
pickerView.setContentHuggingPriority(.defaultLow, for: .horizontal)
在视图中:
MultiComponentsPicker(hours: $hours, minutes: $minutes, seconds: $seconds)
.frame(width: .infinity) // or fixed size