swiftfirebasecontextmenucustom-contextmenuuicontextmenuinteraction

ContextMenuConfiguration for User Images?


I want the image of users to popup (without action option) and then be dismissed if touched outside.

Say table view consist of 2 UI elements (a button and a text) and one picture. How would you set the pop up context menu for only the picture - immy?

public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as!  ViewControllerTableViewCell
    let immy = cell.viewWithTag(1) as! UIImageView
    let person: Userx = people[indexPath.row]
    let button = cell.viewWithTag(3) as! UIButton
    cell.lblName.text = person.Education
    cell.postID = self.people[indexPath.row].postID
    if let PhotoPosts = person.PhotoPosts {
        let url = URL(string: PhotoPosts)
        print(PhotoPosts, "sttdb")
        immy.sd_setImage(with: url)
    }
    return cell
}

extension homepage {
    func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
        let configuration = UIContextMenuConfiguration(identifier: nil, previewProvider: nil)
        return configuration
    }
}

Below is the ViewControlleTableViewCell, added after the first comment. My reply to the comment also explains how this looks in main.storyboard.

class ViewControllerTableViewCell: UITableViewCell {    

    @IBOutlet weak var button: UIButton!
    @IBOutlet weak var immy: UIImageView!
    @IBOutlet weak var lblgenre1: UILabel!    
    @IBOutlet weak var lblName: UILabel!
    @IBOutlet weak var lblName1: UILabel!
    @IBOutlet weak var likeLabel: UILabel!

    var postID: String!
    var caption1: String!
    var keyToPost: String!
    var latestLocation1: [String: Double]?
    let databaseRef = Database.database().reference()
    var refArtists: DatabaseReference!

    let profileImageView: UIImageView = {
        let imageView = UIImageView()
        
        return imageView
    } ()
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

    @IBAction func button1(_ sender: Any) {

    }
    @IBAction func button2NBTHISISTORPREVENTSEGUE(_ sender: Any) {

    }
}

In homepage:

 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ViewControllerTableViewCell

Solution

  • What you need is to enable user interaction in your UIImageView, make your ViewControllerTableViewCell conform to UIContextMenuInteractionDelegate, create an UIContextMenuInteraction and set your cell as the delegate. Then add the iteraction to your image view. Don't forget to remove the contextMenuInteraction method from your HomePage extension.

    Your ViewControllerTableViewCell contextMenuInteraction interaction implementation should look like this:

    import UIKit
    
    class ViewControllerTableViewCell: UITableViewCell, UIContextMenuInteractionDelegate {
        @IBOutlet weak var immy: UIImageView!
        override func awakeFromNib() {
            super.awakeFromNib()
            immy.isUserInteractionEnabled = true
            immy.addInteraction(UIContextMenuInteraction(delegate: self))
        }
        override func setSelected(_ selected: Bool, animated: Bool) {
            super.setSelected(selected, animated: animated)
        }
        func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
            UIContextMenuConfiguration(identifier: nil, previewProvider: nil)  { _ in
                let share = UIAction(title: "Share", image: UIImage(systemName: "square.and.arrow.up")) { _ in
                   // share code
                }
                return UIMenu(title: "Profile Picture Menu", children: [share])
            }
        }
    }