[ad_1]
How do I modify the body measurement in a UIPickerView in swift?
I am making an attempt to make use of a UIPickerView in iPad. However the default measurement of the UIPickerView is small for the iPad display.
I did altering the font measurement bigger, however could not change the picker measurement(not body), and the textual content’s edge was reduce off within the center.
I’ve learn loads of questions, I could not discover anybody else asking the identical query.
How do I modify UIPickerView measurement? Thanks!
The code
import SwiftUI
struct ContentView: View {
personal var myList: [String] = [
"AAAAAAAAAA",
"BBBBBBBBBB",
"CCCCCCCCCC",
"DDDDDDDDDD",
"EEEEEEEEEE"
]
@State var chosen: Int = 0
var physique: some View {
VStack{
Spacer()
MyPicker(myList: myList, chosen: $chosen)
}
}
}
struct MyPicker: View {
var myList: [String]
@Binding var chosen: Int
var physique: some View {
VStack{
PickerView(information: myList, picks: self.$chosen)
.body(width:UIScreen.essential.bounds.width, top: UIScreen.essential.bounds.top/2, alignment: .heart)
.background(Coloration.white)
.padding()
}
}
}
struct PickerView: UIViewRepresentable {
var information: [String]
@Binding var picks: Int
func makeCoordinator() -> PickerView.Coordinator {
Coordinator(self)
}
func makeUIView(context: UIViewRepresentableContext<PickerView>) -> UIPickerView {
let picker = UIPickerView(body: .zero)
picker.dataSource = context.coordinator
picker.delegate = context.coordinator
return picker
}
func updateUIView(_ view: UIPickerView, context: UIViewRepresentableContext<PickerView>) {
// for i in 0...(self.picks.depend - 1) {
// view.selectRow(self.picks[i], inComponent: i, animated: false)
// }
}
class Coordinator: NSObject, UIPickerViewDataSource, UIPickerViewDelegate {
var father or mother: PickerView
init(_ pickerView: PickerView) {
self.father or mother = pickerView
}
func pickerView(_ pickerView: UIPickerView, widthForComponent element: Int) -> CGFloat {
return UIScreen.essential.bounds.width/3
}
func pickerView(_ pickerView: UIPickerView, rowHeightForComponent element: Int) -> CGFloat {
return 80
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent element: Int) -> Int {
return self.father or mother.information.depend
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent element: Int) -> String? {
return self.father or mother.information[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent element: Int) {
//self.father or mother.picks[component] = row
}
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent element: Int, reusing view: UIView?) -> UIView {
let view = UIView(body: CGRect(x: 0, y: 0, width: UIScreen.essential.bounds.width/3, top: 80))
let label = UILabel(body: CGRect(x: 0, y: 0, width: view.bounds.width, top: view.bounds.top))
label.font = UIFont.systemFont(ofSize: 60)
label.textAlignment = NSTextAlignment.heart
label.numberOfLines = 0
label.adjustsFontSizeToFitWidth = true
label.textual content = self.father or mother.information[row]
view.addSubview(label)
return view
}
}
}
[ad_2]
