在UIActionSheet中创建UIPickerView可以通过以下步骤实现:
func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
if buttonIndex == 0 {
let pickerView = UIPickerView(frame: CGRect(x: 0, y: 0, width: 320, height: 216))
pickerView.delegate = self
pickerView.dataSource = self
actionSheet.addSubview(pickerView)
}
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return options.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return options[row]
}
func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
if buttonIndex == 0 {
let pickerView = UIPickerView(frame: CGRect(x: 0, y: 0, width: 320, height: 216))
pickerView.delegate = self
pickerView.dataSource = self
actionSheet.addSubview(pickerView)
// 添加布局约束
pickerView.translatesAutoresizingMaskIntoConstraints = false
actionSheet.addConstraint(NSLayoutConstraint(item: pickerView, attribute: .centerX, relatedBy: .equal, toItem: actionSheet, attribute: .centerX, multiplier: 1, constant: 0))
actionSheet.addConstraint(NSLayoutConstraint(item: pickerView, attribute: .bottom, relatedBy: .equal, toItem: actionSheet, attribute: .bottom, multiplier: 1, constant: 0))
actionSheet.addConstraint(NSLayoutConstraint(item: pickerView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 320))
actionSheet.addConstraint(NSLayoutConstraint(item: pickerView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 216))
}
}
这样就可以在UIActionSheet中创建UIPickerView了。
领取专属 10元无门槛券
手把手带您无忧上云