在iPad上单击条形按钮项目时不显示UIAction表

时间:2017-08-02 11:29:41

标签: ios swift3 uialertcontroller

我已经以编程方式创建了一个带有左右栏按钮项的导航栏。单击右侧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)
    }

1 个答案:

答案 0 :(得分:0)

//设置带有图片的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)
}