Я хочу добавить выпадающий список в контроллере представления без использования раскадровки. Я написал этот код, но он не работает. Пожалуйста, помогите мне.
let pickerview: UIPickerView = UIPickerView(frame: CGRect(x: 10, y: 30, width: 20, height: 10))
self.view.addSubview(pickerview)
let sensorView = UIView()
var scrollView = UIScrollView()
sensorView.frame = CGRect(x: 35, y: 20, width: 280, height: 140)
sensorView.layer.cornerRadius = 0
sensorView.clipsToBounds = true
sensorView.isUserInteractionEnabled = false
let sensorOptions = ["Weather Sensor", "Humidity Sensor"]
scrollView = UIScrollView(frame: CGRect(x: 70, y: 270, width: 430, height: 160))
scrollView.delegate = self as? UIScrollViewDelegate
scrollView.isScrollEnabled = true
let buttonHeight: CGFloat = UIScreen.main.bounds.height / 15
let contentHeight: CGFloat = CGFloat(sensorOptions.count) * buttonHeight
scrollView.contentSize = CGSize(width: UIScreen.main.bounds.width / 2.0, height: contentHeight)
for (index, title) in sensorOptions.enumerated()
{
let button = UIButton(type: .custom)
button.backgroundColor = .white
button.setTitleColor(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 1), for: .normal)
button.frame = CGRect(x: 0, y: buttonHeight * CGFloat(index), width: UIScreen.main.bounds.width / 2.0,
height: buttonHeight)
button.setTitle(title, for: .normal)
sensorView.addSubview(button)
}
//sensorButton.loadDropdownData(sensorOptions)
scrollView.addSubview(sensorView)
view.addSubview(scrollView)