PickerView Custom in Swift programmatically

1.7k Views Asked by At

I'm trying to build a custom PickerView.

a) I would like to remove the two SelectionIndicator lines. I have read that in the latest versions of iOS it is no longer possible to delete these two lines, but I would like to at least change their color to camouflage them.

b) I would like to give rounded corners to the purple background of the selected item.

I've written everything in the code, but it doesn't work. enter image description here

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var viewContainer: UIView!
    @IBOutlet weak var pickerView: UIPickerView!
    @IBOutlet weak var btnCancel: UIButton!
    @IBOutlet weak var btnConfirmation: UIButton!
    @IBOutlet weak var lb: UILabel!
    
    var list = ["Madrid", "Sidney", "Toronto", "NewYork", "Oslo"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        pickerView.dataSource = self
        pickerView.delegate = self
        
        viewContainer.layer.cornerRadius = 40
        viewContainer.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        viewContainer.layer.shadowColor = #colorLiteral(red: 0.7647058824, green: 0.7647058824, blue: 0.7647058824, alpha: 1)
        viewContainer.layer.shadowOpacity = 0.7
        viewContainer.layer.shadowRadius = 10
        
        btnCancel.setTitle("Cancel", for: .normal)
        btnCancel.titleLabel?.font = UIFont(name: "Helvetica", size: CGFloat(16))
        btnCancel.setTitleColor(.white, for: .normal)
        btnCancel.backgroundColor = #colorLiteral(red: 0.6829224825, green: 0.6091368198, blue: 0.9736139178, alpha: 1)
        btnCancel.layer.cornerRadius = btnCancel.frame.height/2.0
        btnCancel.addTarget(self, action: #selector(tapBtnCancel(_ :)), for: .touchUpInside)
        
        btnConfirmation.setTitle("Confirm", for: .normal)
        btnConfirmation.titleLabel?.font = UIFont(name: "Helvetica", size: CGFloat(16))
        btnConfirmation.setTitleColor(.white, for: .normal)
        btnConfirmation.backgroundColor = #colorLiteral(red: 0.6705882353, green: 0.6117647059, blue: 0.9490196078, alpha: 1)
        btnConfirmation.layer.cornerRadius = btnConfirmation.frame.height/2.0
        btnConfirmation.addTarget(self, action: #selector(tapBtnConfirmation(_ :)), for: .touchUpInside)
        
    }
    
    @objc func tapBtnCancel(_ sender: UIButton) {
        
    }
    
    @objc func tapBtnConfirmation(_ sender: UIButton) {
        lb.text = list[self.pickerView.selectedRow(inComponent: 0)]
    }


}



extension ViewController: UIPickerViewDataSource {
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return list.count
    }
   
}

extension ViewController: UIPickerViewDelegate {
    
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 40
    }
    
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        
        let pickerLabel = UILabel()
        pickerLabel.textColor = #colorLiteral(red: 0.6829224825, green: 0.6091368198, blue: 0.9736139178, alpha: 1)
        pickerLabel.text = list[row]
        pickerLabel.font = UIFont(name: "Helvetica", size: 22)
        pickerLabel.textAlignment = NSTextAlignment.center
        
        pickerLabel.layer.cornerRadius = pickerLabel.frame.height/2.0
        pickerLabel.backgroundColor = #colorLiteral(red: 0.9468494058, green: 0.9369241595, blue: 1, alpha: 1)
        
        return pickerLabel
    }
   
}
0

There are 0 best solutions below