UIPickerView 与 UIViewRepresentable 不填充整个宽度

发布于 2025-01-14 11:48:31 字数 3108 浏览 6 评论 0原文

不幸的是,当我使用 UIViewRepresenable 在 SwiftUI 中创建 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)!
            }
        }
    }
}

这就是它的样子:

在此处输入图像描述

Unfortunately, when I create a UIPickerView in SwiftUI with UIViewRepresenable it doesn't fill the whole screen. I've also tried giving it a lot of frame modifiers, but nothing worked. Does anyone have a solution for this or knows why the width of the entire screen is not filled with the picker? Best regards

This is the Code:

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)!
            }
        }
    }
}

And thats what it looks like:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请持续率性 2025-01-21 11:48:31

在 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

in the UIViewRepresentable:

    // allows rows to be compressed
    pickerView.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
    // allows rows to be expanded
    pickerView.setContentHuggingPriority(.defaultLow, for: .horizontal)

in the View:

    MultiComponentsPicker(hours: $hours, minutes: $minutes, seconds: $seconds)
        .frame(width: .infinity) // or fixed size
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文