'How do I change the frame size in a UIPickerView?

How do I change the frame size in a UIPickerView in Swift?

I'm trying to use a UIPickerView on an iPad but the default size of the UIPickerView is too small for the iPad screen.

I tried making the font size larger, but couldn't change the picker size (not frame), and the text's edge was cut off in the middle.

I have read a lot of questions but I couldn't find any asking the same thing.

How do I change the UIPickerView size?

The code

import SwiftUI

struct ContentView: View {
    
    private var myList: [String] = [
        "AAAAAAAAAA",
        "BBBBBBBBBB",
        "CCCCCCCCCC",
        "DDDDDDDDDD",
        "EEEEEEEEEE"
    ]
    @State var selected: Int = 0
    
    var body: some View {
        VStack{
            Spacer()
            MyPicker(myList: myList, selected: $selected)
        }
    }
}

struct MyPicker: View {
    var myList: [String]
    @Binding var selected: Int
    
    var body: some View {
        VStack{
            PickerView(data: myList, selections: self.$selected)
                .frame(width:UIScreen.main.bounds.width, height: UIScreen.main.bounds.height/2, alignment: .center)
                .background(Color.white)
                .padding()
        }
    }
}


struct PickerView: UIViewRepresentable {
    var data: [String]
    @Binding var selections: Int

    func makeCoordinator() -> PickerView.Coordinator {
        Coordinator(self)
    }

    func makeUIView(context: UIViewRepresentableContext<PickerView>) -> UIPickerView {
        let picker = UIPickerView(frame: .zero)
        
        picker.dataSource = context.coordinator
        picker.delegate = context.coordinator
        
        
        
        return picker
    }

    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(_ pickerView: PickerView) {
            self.parent = pickerView
            
            
        }
        
        func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
            return UIScreen.main.bounds.width/3
        }
        
        func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
            return 80
        }

        func numberOfComponents(in pickerView: UIPickerView) -> Int {
            return 1
        }

        func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
            return self.parent.data.count
        }

        func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
            return self.parent.data[row]
        }

        func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
            //self.parent.selections[component] = row
        }
        
        func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
            let view = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width/3, height: 80))
            let label = UILabel(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height))

            label.font = UIFont.systemFont(ofSize: 60)
            label.textAlignment = NSTextAlignment.center
            label.numberOfLines = 0
            label.adjustsFontSizeToFitWidth = true
            label.text = self.parent.data[row]
            
            view.addSubview(label)
            
            return view
        }
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source