parent
컨테이너 UIView에는 UITextView 1 개와 버튼 2 개 (버튼은 UITextView 위에 있지만 컨테이너 UIView의 하위 뷰로 포함됨)의 세 가지 하위 뷰가 있습니다. 일반적으로 UIViewController 내부에 부모 UIView를 화면에 표시하면 버튼이 관련 액션 메서드를 완벽하게 트리거하고 올바르게 작동합니다. 그러나 popover가되는 뷰 컨트롤러 안에 UIView를 표시하면 뷰 계층 구조가 제대로 표시되지만 단추는 관련 작업 메서드를 트리거하지 않으며 아무 일도 발생하지 않습니다. UIPopoverPresentationController
과 내가 이해하지 못하는 버튼에 대해 뭔가가 있습니까? 의 UIViewController을 제시 부모의 UIViewUIButton은 정상적으로 작동하지만 popoverPresentationController에서는 작동하지 않습니다.
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
}
에 버튼을 추가
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 - 나는 func present(viewController:at:)
의 마지막 줄에 내가했습니다 아래 popController?.delegate = viewController
를 추가했습니다 덧붙여서 extension UIViewController: UIPopoverPresentationControllerDelegate { }
내가 업데이트 한 귀하의 제안을 반영하기 위해 질문 : 당신의 질문에 대답하기 위해, 당신은 위임하도록 제시 뷰 컨트롤러에게, 그래서 아래이 추가하고 작동하는지 알려해야합니다. 그러나 여전히'viewController'를 델리게이트로 추가하고'UIViewController'를'UIPopoverPresentationControllerDelegate'에 맞게 확장 한 후에도 작동하지 않습니다 –
, popoverPresentationController가 제대로 나타나면 모든 UIButton이 작동해야합니다. 따라서 popover가 소스로 사용하는 "viewController"를 보지 않고도 "viewController"클래스에서 약간의 실수를 저지른 것 같아요. – Giraffe