如何使用 tabBarItems 在 ipad actionSheet 中显示 pickerView

How to show pickerView in ipad actionSheet using tabBarItems

我试图在 ios 的 actionSheet 中添加 pickerView,并成功地显示在 iPhone 如下所示的图片中: this is on iphone8 and actionSheet is working as expected

this is on iPad and pickerView is not displaying anything

我实现的代码如下:

class HomeViewController: UIViewController, UITabBarDelegate, UIActionSheetDelegate, UIPickerViewDelegate, UIPickerViewDataSource{
    let CheckInList = ["Site 1","Site 2","Site 3","Site 4"]
    @IBOutlet weak var homeTabBar: UITabBar!
    override func viewDidLoad() {
        super.viewDidLoad()
        homeTabBar.delegate = self;
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return CheckInList.count
    }
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return CheckInList[row]
    }
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 40
    }
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        UserDefaults.standard.set(CheckInList[row], forKey: "CheckInSelected")
    }
}

这就是我实现 tabBarItem 代码的方式

func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    print(item.title!)
    switch item.title! {
    case "Check in":
        self.checkInAction()
        break
    default:
        break
    }
}

这是我的 checkInAction 函数:

func checkInAction(){
    let alertView: UIAlertController = UIAlertController(title: "CheckIn", message: "", preferredStyle: .actionSheet)
    let height:NSLayoutConstraint = NSLayoutConstraint(item: alertView.view, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: self.view.frame.height * 0.35)
    alertView.view.addConstraint(height);

    let pickerView: UIPickerView = UIPickerView(frame: CGRect(x: 0, y: 10, width: alertView.view.frame.width - 18, height: height.constant - 60));
    pickerView.delegate = self
    pickerView.dataSource = self

    if let row = CheckInList.firstIndex(of: selected) {
        pickerView.selectRow(row, inComponent: 0, animated: false)
    }
    alertView.view.addSubview(pickerView)

    let action = UIAlertAction(title: "OK", style: .default, handler: nil)
    alertView.addAction(action)

    if let popoverController = alertView.popoverPresentationController {
        popoverController.sourceView = self.view
        popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
        popoverController.permittedArrowDirections = []
    }

    self.present(alertView, animated: true, completion: nil)
}

我需要一些帮助来解决 ipad 上的这个问题。通过我的研究,我知道在 ios ipad 上我们无法显示操作 sheet,我的研究来自核心文档和一些帮助链接。

据我所知,代码或 ipad 没有问题 只是你给 UIPickerView 设置了错误的宽度

只需将您的代码替换为此

 let pickerView: UIPickerView = UIPickerView(frame: CGRect(x: 0, y: 10, width: 270, height: height.constant - 60));

width: 270 根据您的要求更改 但是(270 宽 x 144 高)这是 uialert

的默认大小

当你设置 UIPickerView(frame: CGRect(x: 0, y: 10, width: alertView.view.frame.width - 18, height: height.constant - 60));

这意味着它从 0 开始并采用整个视图宽度 (-18),这在 actionSheet 方面是正确的,但在警报方面却不是。

这解决了我的问题,只是想与未来的朋友分享解决方案。

 if let popoverController = alertView.popoverPresentationController {
        popoverController.sourceView = self.view
        popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
        popoverController.permittedArrowDirections = []

        height = NSLayoutConstraint(item: alertView.view, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: self.view.frame.height * 0.25)
         pickerView.frame = CGRect(x: 0, y: 10, width: 300, height: height.constant - 60);
    }

我将 pickerview 框架宽度更改为 300 并重新计算了此 popOverController 中的高度,这解决了我的问题。