如何使用tabBar项在ipad操作表中显示uipickerView

问题描述 投票:0回答:2

我试图在ios的actionSheet中添加pickerView,并成功显示如下所示的iPhone图片:qazxsw poi

this is on iphone8 and actionSheet is working as expected

我实现的代码如下:

this is on iPad and pickerView is not displaying anything

这是我实现tabBarItem代码的方式

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")
    }
}

这是我的checkInAction函数:

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

我想帮助解决ipad上的这个问题。我知道我的研究,在ios ipad上我们无法显示行动表,而我的研究来自核心文档和一些帮助链接。

ios swift uialertcontroller
2个回答
1
投票

根据我的知识,在代码或ipad中没有问题只是你给UIPickerView提供了错误的宽度

只需用此替换您的代码即可

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)
}

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

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

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

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


0
投票

这解决了我的问题,只是想与配偶分享解决方案以备将来使用。

enter image description here

我将pickerview框架宽度更改为300并重新计算了这个修复我的问题的popOverController的高度。

© www.soinside.com 2019 - 2024. All rights reserved.