gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 13:50:08 24 4
gpt4 key购买 nike

我试图在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com