【问题标题】:UIButton works normally, but not in popoverPresentationControllerUIButton 工作正常,但在 popoverPresentationController 中不行
【发布时间】:2017-01-13 17:46:11
【问题描述】:

我有一个parent 容器 UIView,它具有三个子视图:1 个 UITextView 和 2 个按钮(这些按钮位于 UITextView 的顶部,但包含在容器 UIView 的子视图中)。当我在屏幕上正常显示父 UIView 时,只需在 UIViewController 内部,按钮就会完美地触发其关联的操作方法并正常运行。但是,当我在成为弹出框的视图控制器中显示 UIView 时,视图层次结构会正确显示,但按钮不会触发它们关联的操作方法,并且没有任何反应。 UIPopoverPresentationControllers 和按钮有什么我不明白的地方吗?

将按钮添加到父 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

}

呈现 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

}

更新 - 我添加了 popController?.delegate = viewController 作为 func present(viewController:at:) 的最后一行,下面我添加了 extension UIViewController: UIPopoverPresentationControllerDelegate { }

【问题讨论】:

    标签: ios swift uiview uibutton uipopovercontroller


    【解决方案1】:

    要回答你的问题,你需要告诉你的呈现视图控制器委托谁,所以在下面添加这个,让我知道它是否有效:

    popController?.delegate = viewController
    

    【讨论】:

    • 我已更新问题以反映您的建议。但是,在将viewController 添加为委托并扩展UIViewController 以符合UIPopoverPresentationControllerDelegate 后,它仍然不起作用
    • 呵呵,如果你的 popoverPresentationController 确实弹出了,那么所有的 UIButton 都应该可以工作。因此,如果没有看到您的弹出窗口来源的“viewController”,我猜您可能在您的“viewController”类上犯了一些相当简单的错误。
    猜你喜欢
    • 1970-01-01
    • 2018-06-08
    • 1970-01-01
    • 2013-06-29
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-25
    相关资源
    最近更新 更多