本文介绍了SwiftUI:设置拾取器行高的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于大字体,Picker中的行重叠。如何更改Picker's行高?(提示:.lineSpacing修饰符不执行此操作。)
另见
此问题类似于Ejaaz的问题,但他的问题到目前为止仍未得到回答。
问题
代码
以下可运行代码产生上述结果。我真的不想要不同大小的线条,我只想要大字体合适。我已经尝试插入Spacers,在这里和那里添加.frame修饰语,.lineSpacing,padding()...也许只是没有找到正确的组合?
struct ContentView: View { @State private var selected = 0 var body: some View { Picker(selection: self.$selected, label: Text("Letters")) { Text("A").font(.system(size: 30)) Text("B").font(.system(size: 40)) Text("C").font(.system(size: 50)) Text("D").font(.system(size: 60)) Text("E").font(.system(size: 70)) Text("F").font(.system(size: 80)) } } } 推荐答案您必须将UIPickerView包装在UIVieweRenatable中,然后使用
func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat更改您的鼠标右键。
示例:(它只是展示了如何更改鼠标右键,您仍然需要为您的内容扩展它...)
struct PickerView: UIViewRepresentable { var data: [[String]] @Binding var selections: Int //makeCoordinator() func makeCoordinator() -> PickerView.Coordinator { Coordinator(self) } //makeUIView(context:) func makeUIView(context: UIViewRepresentableContext<PickerView>) -> UIPickerView { let picker = UIPickerView(frame: .zero) picker.dataSource = context.coordinator picker.delegate = context.coordinator return picker } //updateUIView(_:context:) func updateUIView(_ view: UIPickerView, context: UIViewRepresentableContext<PickerView>) { // for i in 0...(self.selections.count - 1) { // view.selectRow(self.selections[i], inComponent: i, animated: false) // } } class Coordinator: NSObject, UIPickerViewDataSource, UIPickerViewDelegate { var parent: PickerView //init(_:) init(_ pickerView: PickerView) { self.parent = pickerView } func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat { return 80 } //numberOfComponents(in:) func numberOfComponents(in pickerView: UIPickerView) -> Int { return self.parent.data.count } //pickerView(_:numberOfRowsInComponent:) func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { return self.parent.data[component].count } //pickerView(_:titleForRow:forComponent:) func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { return self.parent.data[component][row] } //pickerView(_:didSelectRow:inComponent:) func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { // self.parent.selections[component] = row } } }更多推荐
SwiftUI:设置拾取器行高
发布评论