UIAction 工作表未在 iPad 中单击条形按钮项目时关闭
Posted
技术标签:
【中文标题】UIAction 工作表未在 iPad 中单击条形按钮项目时关闭【英文标题】:UIAction sheet not dismissing on bar button item click in iPad 【发布时间】:2017-08-02 11:29:41 【问题描述】:我以编程方式创建了一个带有左右栏按钮项的导航栏。单击右侧UIBarButtonItem
时,将显示操作表,然后如果我单击左键,则操作表仍然存在。它不会解雇。但是,如果我点击视图控制器中的其他任何地方,动作表就会被取消。该问题仅适用于 iPad。这是右栏按钮单击时调用的函数。
func showActionButtons()
actionSheetController = UIAlertController(title: Constants.kPleaseSelect, message: nil, preferredStyle: .actionSheet)
let cancelActionButton = UIAlertAction(title: Constants.kCancel, style: .cancel) action -> Void in
actionSheetController.addAction(cancelActionButton)
let cameraActionButton = UIAlertAction(title: "Camera", style: .default) action -> Void in
//open camera
self.checkCamera()
actionSheetController.addAction(cameraActionButton)
let galleryActionButton = UIAlertAction(title: "Gallery", style: .default) action -> Void in
//open gallery
self.checkGallery()
actionSheetController.addAction(galleryActionButton)
actionSheetController.popoverPresentationController?.barButtonItem = self.navigationItem.rightBarButtonItem
self.present(actionSheetController, animated: true, completion: nil)
【问题讨论】:
显示你尝试过的代码 在 UIViewController 中你是否打开了 UIActionSheet? 在 ViewController 中,我有 2 个条形按钮项,单击右侧条形按钮显示操作表。单击左侧条形按钮项时,我无法关闭操作表。 @lakshmi - 只显示你尝试过的代码,UIbarbutton 没有任何 UIcontrols hiearchy,所以添加你的代码 试试这个: if (self.respondsToSelector(Selector("popoverPresentationController"))) self.popoverPresentationController?.passthroughViews = nil else // 对于ios8-pre版本,我们需要传递popoverController来自 segue self.popoverController?.passthroughViews = nil l 的引用 【参考方案1】://设置RightBar Item带图片
private func setRightBarSelectAllItem()
let image = UIImage(named: "ic_nav_select_all")?.withRenderingMode(.alwaysOriginal)
let backItem = UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(onBtnSelectAll(_:)))
self.navigationItem.rightBarButtonItem = backItem
//IBAction
@IBAction func onBtnSelectAll(_ sender: UIBarButtonItem)
showActionSheet(sender: sender)
//动作表
private func showActionSheet(sender : UIBarButtonItem)
let alt = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let select = UIAlertAction(title: "Select", style: .default) (act) in
let selectAll = UIAlertAction(title: "Select All", style: .default) (act) in
let deleteAll = UIAlertAction(title: "Delete All", style: .destructive) (act) in
let cancel = UIAlertAction(title: "Cancel", style: .cancel) (act) in
alt.addAction(select)
alt.addAction(selectAll)
alt.addAction(deleteAll)
alt.addAction(cancel)
alt.popoverPresentationController?.barButtonItem = sender
self.present(alt, animated: true, completion: nil)
【讨论】:
以上是关于UIAction 工作表未在 iPad 中单击条形按钮项目时关闭的主要内容,如果未能解决你的问题,请参考以下文章