Jeremy Kelleher
Jeremy Kelleher

Reputation: 287

UIButton works normally, but not in popoverPresentationController

I have a parent container UIView that has three subviews: 1 UITextView, and 2 buttons (the buttons are on top of the UITextView, but contained as subviews of the container UIView). When I present the parent UIView on screen normally, simply inside of a UIViewController, the buttons trigger their associated action methods perfectly and function correctly. However, when I present the UIView inside a view controller that becomes a popover, the view hierarchy shows up properly, but the buttons don't trigger their associated action methods and nothing happens. Is there something about UIPopoverPresentationControllers and buttons that I don't understand?

Adding the buttons to the parent UIView

func layoutButtons(in parent: UIView) {
    let speechButton = UIButton(type: .custom)
    speechButton.frame = CGRect(x: parent.frame.width - 35, y: parent.frame.height - 35, width: 30, height: 30)
    speechButton.setImage(UIImage(named: "sound_icon"), for: .normal)
    speechButton.addTarget(self, action: #selector(Textual.textToSpeech), for: UIControlEvents.touchUpInside)
    parent.addSubview(speechButton)

    let fontSizeButton = UIButton(type: .custom)
    fontSizeButton.frame = CGRect(x: textView.frame.width - 75, y: textView.frame.height - 35, width: 30, height: 30)
    fontSizeButton.setImage(UIImage(named: "font_size_icon"), for: .normal)
    fontSizeButton.addTarget(self, action: #selector(Textual.toggleFontSize), for: UIControlEvents.touchUpInside)
    parent.addSubview(fontSizeButton)

    // wrap text around the bottom buttons
    let excludeSpeechButton = UIBezierPath(rect: speechButton.frame)
    let excludeFontSizeButton = UIBezierPath(rect: fontSizeButton.frame)
    self.textView.textContainer.exclusionPaths = [excludeSpeechButton, excludeFontSizeButton]
}

@objc func textToSpeech() {
    if synth.isPaused {
        synth.continueSpeaking()
    } else if synth.isSpeaking {
        synth.pauseSpeaking(at: .immediate)
    } else {
        let speechUtterance = AVSpeechUtterance(string: attributedText.string)
        speechUtterance.rate = 0.5
        synth.speak(speechUtterance)
    }
}

@objc func toggleFontSize() {

    if self.smallFontSizeMode == true {
        self.smallFontSizeMode = false
    } else {
        self.smallFontSizeMode = true
    }

    // for each character, multiply the current font size by fontSizeModifier
    // http://stackoverflow.com/questions/19386849/looping-through-nsattributedstring-attributes-to-increase-font-size

    self.attributedText.beginEditing()

    self.attributedText.enumerateAttribute(NSFontAttributeName, in: NSMakeRange(0, self.attributedText.length), options: NSAttributedString.EnumerationOptions.reverse, using: { (value, range, stop) in
        if let oldFont = value as? UIFont {
            var newFont: UIFont?
            if self.smallFontSizeMode == true { // was big and now toggling to small
                newFont = oldFont.withSize(oldFont.pointSize / 2)
            } else { // was small and now toggling to big
                newFont = oldFont.withSize(oldFont.pointSize * 2)
            }
            attributedText.removeAttribute(NSFontAttributeName, range: range)
            attributedText.addAttribute(NSFontAttributeName, value: newFont!, range: range)
        }
    })

    self.attributedText.endEditing()

    self.textView.attributedText = self.attributedText

}

Presenting the UIViewController

func present(viewController: UIViewController, at location: CGRect) {

    viewController.modalPresentationStyle = .popover
    parentViewController.present(viewController, animated: true, completion: nil)

    let popController = viewController.popoverPresentationController
    popController?.permittedArrowDirections = .any
    popController?.sourceView = parentView
    popController?.sourceRect = location

}

UPDATE - I've added popController?.delegate = viewController as the last line of func present(viewController:at:) and below I've added extension UIViewController: UIPopoverPresentationControllerDelegate { }

Upvotes: 1

Views: 307

Answers (1)

Giraffe
Giraffe

Reputation: 149

To answer your question, you will need to tell your presenting view controller who to delegate, so add this below and let me know if it works:

popController?.delegate = viewController

Upvotes: 0

Related Questions