0

I would like to modify the height of UIPicker programmatically, I tried to do this but on the simulator the height remains as it was before, I don't see any changes.

@objc open func showPicker(title: String?, selected: String?, strings:[String], color: UIColor? = nil, completion:DPPickerValueIndexCompletion?) {
        self.pickerValues = strings
        
        let picker = UIPickerView()
        picker.delegate = self
        picker.dataSource = self
        
        
        picker.transform = CGAffineTransformMakeScale(0.5, 0.5)
        
        
        
        if let value = selected {
            picker.reloadAllComponents()
            if strings.count > 0 {
                OperationQueue.current?.addOperation {
                    let index = strings.firstIndex(of: value) ?? 0
                    picker.selectRow(index, inComponent: 0, animated: false)
                }
            }
        }

        self.showPicker(title: title, view: picker, color: color) { (cancel) in
            
            var index = -1
            var value: String? = nil
            
            if !cancel, strings.count > 0 {
                index = picker.selectedRow(inComponent: 0)
                if index >= 0 {
                    value = self.pickerValues?[index]
                }
            }
            
            completion?(value, index, cancel || index < 0)
        }
    }
General Grievance
  • 4,555
  • 31
  • 31
  • 45
iosSwift
  • 31
  • 6

1 Answers1

0

I don't why it's not resizing itself properly when you translate autoresizing mask into constraints, but it works properly when you use AutoLayout:

    let pickerView = UIPickerView()
    pickerView.translatesAutoresizingMaskIntoConstraints = false
    pickerView.delegate = self
    pickerView.dataSource = self

    view.addSubview(pickerView)
    NSLayoutConstraint.activate([
        pickerView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        pickerView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
        pickerView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
        pickerView.heightAnchor.constraint(equalToConstant: 500)
    ])
Bulat Yakupov
  • 440
  • 4
  • 13