lakshmi
lakshmi

Reputation: 31

UIAction sheet not dismissing on bar button item click in iPad

I have programmatically created a navigation bar with a left and right bar button items. On clicking the right UIBarButtonItem the action sheet is presented,and then if i click left button,the action sheet persists. It does not dismiss. But if I tap anywhere else in the view controller the actionsheet is dismissed. The issue is only for iPad. This is the function called on the right bar button click.

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

Upvotes: 0

Views: 336

Answers (1)

Milan Savaliya M
Milan Savaliya M

Reputation: 304

//Set RightBar Item with image

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

//ActionSheet

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

Upvotes: 0

Related Questions