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

标签 ios swift uialertcontroller

我试图在 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 上我们无法显示操作表,我的研究来自核心文档和一些帮助链接。

最佳答案

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

只需用这个替换你的代码

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

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

enter image description here

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

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

关于ios - 如何使用 tabBarItems 在 ipad actionSheet 中显示 pickerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54779756/

相关文章:

ios - 将视频转换为mp4格式

ios - 修改 UISearchBar 取消按钮字体文字颜色和样式

php - PHP API 无法识别 IOS HTTPCookie

swift - 如何快速将两个 [String] 映射为一个 [Record]?

ios - 如何在 Skscene 中呈现 UIAlert

ios - 如何更改 objective-c 中的字体样式和颜色或操作表?

c# - 需要知道如何在 Xamarin.iOS 中终止后台进程

ios - 实现react-native-fbsdkshare

ios - IBDesignable UIView 子类总是无法在 Interface Builder 中呈现

ios - 按下按钮时关闭警报 Swift