如何使用tabBarItems在

时间:2019-02-20 06:00:16

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

2 个答案:

答案 0 :(得分:1)

据我所知,代码或ipad中没有问题 只是您给UIPickerView指定了错误的宽度

仅以此替换您的代码

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

宽度: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开始并采用整个视图宽度(-18),这在actionSheet上是正确的,但在警告中却不正确。

答案 1 :(得分:0)

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

 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中重新计算了高度,从而解决了我的问题。