corin8823 / Popover

Popover is a balloon library like Facebook app. It is written in pure swift.
MIT License
2.1k stars 327 forks source link

Not able to dismiss the popover #73

Closed BhaktiKarva closed 7 years ago

BhaktiKarva commented 7 years ago

Hello, I am displaying a popover on a gesture recogniser. The popover shows a custom collectionview. But i am not able to dismiss the popover by using popover.dismiss.

Here is my code:

` class KeyboardViewController: UIInputViewController , ISEmojiViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource {

var extras : [String] = []

fileprivate var popover1: Popover!

//setting up everything

func ShowPop(_ sender: UILongPressGestureRecognizer) {
    self.popover1 = Popover(options: [.type(.up), .showBlackOverlay(false)])
    self.popover1.willShowHandler = {
        print("willShowHandler")
    }
    self.popover1.didShowHandler = {
        print("didshowHandler")
    }
    self.popover1.willDismissHandler = {
        print("willDismissHandler")
    }
    self.popover1.didDismissHandler = {
        print("didDismissHandler")
    }
    if sender.state == .began {
        //get the button
        if let button = sender.view as? UIButton {
            switch button.tag {
            case 50 : extras = ["è","é","ê","ë","ē"]
                        break
            case 55 : extras = ["ì","í","î","ï"]
                        break
            case 60 : extras = ["ò","ó","ô","õ","ö"]
                        break
            case 65 : extras = ["à","á","â","ã","ä"]
                        break
            case 70 : extras = ["È","É","Ê","Ë","Ē"]
                        break
            case 75 : extras = ["Ì","Í","Î","Ï"]
                        break
            case 80 : extras = ["Ò","Ó","Ô","Õ","Ö"]
                        break
            case 85 : extras = ["À","Á","Â","Ã","Ä"]
                        break
                default:
                        print("none")
            } //end switch
            let fl = UICollectionViewFlowLayout()
            fl.minimumLineSpacing = 0
            fl.minimumInteritemSpacing = 0
            let width = self.view.frame.width / 4
            let aView = UICollectionView(frame: CGRect(x: 0, y: 0, width: width, height: (emojibottom.frame.height)), collectionViewLayout: fl)
            aView.delegate = self
            aView.dataSource = self
            aView.isScrollEnabled = false
            aView.allowsMultipleSelection = false
            aView.register(UINib.init(nibName: "ViewCell", bundle: nil), forCellWithReuseIdentifier: "myCell")
            self.popover1.show(aView, fromView: button, inView: self.view)
        } //end if button
    } //end if state
}

//collection view
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let cell = collectionView.cellForItem(at: indexPath) as! CollectionViewCell
    (textDocumentProxy as UIKeyInput).insertText(cell.tLabel.text!)
    popover1.dismiss()
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if extras.count > 0 {
        return extras.count}
    else {return 0 }
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCell", for: indexPath) as! CollectionViewCell
    cell.tLabel.text = extras[indexPath.row]
    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
    let width = collectionView.frame.width/(CGFloat(extras.count))
    let height = collectionView.frame.height
    return CGSize(width: width, height: height)
}
     }

`

but whenever I select something from the popover, it does not dismiss.

BhaktiKarva commented 7 years ago

I figured that the popover is not going because if self.superview != nil always returns nil and so it does not dismiss the popover. How to solve this?

BhaktiKarva commented 7 years ago

I made a few changes and instead of the collection view, I am adding a UIView with some buttons. The problem is it still does not dismiss the popover. I am getting really confused here. Please help.

Here is my code

//create view
                let aview = UIView(frame: CGRect(x: 0, y: 0, width: (self.view.frame.width/4), height: button.frame.height))
                let width = (self.view.frame.width/4)/(CGFloat(extras.count))
                //adding buttons
                for i in 0...(extras.count-1) {
                    let butt = UIButton(frame: CGRect(x: (CGFloat(i)*width), y: 0, width: width, height: (aview.frame.height)))
                    butt.setTitle(extras[i], for: .normal)
                    butt.setTitleColor(UIColor.black, for: .normal)
                    butt.addTarget(self, action: #selector(keyPressedletter(_:)), for: .touchUpInside)
                    aview.addSubview(butt)
                }

                //show pop-up
                self.popover1.show(aview, fromView: button, inView: self.view)
BhaktiKarva commented 7 years ago

Can you please help me with issue. I really need it to work. :(

corin8823 commented 7 years ago

I think it can be solved if there is your demo project.

BhaktiKarva commented 7 years ago

https://github.com/BhaktiKarva/CustomKeyboard

corin8823 commented 7 years ago

The project is too big to understand the location of the problem. Is it okay for me to just demo the problem?

BhaktiKarva commented 7 years ago

ok. how do you wanna connect? Teamviewer? Skype?

corin8823 commented 7 years ago

That's a bit difficult. (I am not good at English) So can I have a small sample project with only the problem part?

BhaktiKarva commented 7 years ago

in the main project - > Ghanakey-keyboard extension. In that go to the KeyboardViewController. Go the mark //gestures. In that there is a ShowPop method. Everything works fine. But when I try to dismiss the popup, it remains there only. I figured out that it does not dismiss because in the Popover.swift file, in the dismiss() mehtod if self.superview != nil always returns nil and it does not dismiss.

corin8823 commented 7 years ago

https://github.com/corin8823/Popover/commit/c9c08eb436769c381a4eae6d3a8010f25d86e8fb I fixed it with this commit. I could dismiss if it was a current master popover.

corin8823 commented 7 years ago

But I do not dismiss when it is keyPressedletter

BhaktiKarva commented 7 years ago

I tried with your new commit, but it is still not dismissing the popover.

BhaktiKarva commented 7 years ago

If I click anywhere else, it dismiss the popover. But on calling popover.dismiss() is not dismissing the popover. Can you please help me in this?

corin8823 commented 7 years ago

Umm

It works with this example

fileprivate var popoverOptions: [PopoverOption] = [
    .type(.up),
-  .blackOverlayColor(UIColor(white: 0.0, alpha: 0.6))
+  .showBlackOverlay(false)
BhaktiKarva commented 7 years ago

I tried, but still not working. When I click anywhere other than the popover, it dismisses the popover. Bu on clicking any any button, it does not dismiss.

BhaktiKarva commented 7 years ago

I finally got it working. The problem was I was initialising the popover in the @IBAction method. I shifted that to the viewDidLoad method and now it is working fine. Thank you for all the help :)