我有一个弹出窗口,我试图在方向改变时改变宽度。当弹出窗口以当前方向显示时,我得到了我想要的宽度,但是当我从 .portrait -> .landscape 切换时,我没有得到我想要的宽度。例如,它保留 .portrait 一次。
我阅读了有关 popoverPresentationController(_:willRepositionPopoverTo:in:) 的文档
但我不知道它是如何工作的。 实际上,当我改变方向时它甚至不会调用。我使用的 UIPopoverPresentationControllerDelegate 的其余部分可以正常工作。
这是更改 PopUp 大小的正确方法,还是应该使用其他内容?
有什么帮助吗?非常感谢
func popoverPresentationController(_ popoverPresentationController: UIPopoverPresentationController, willRepositionPopoverTo rect: UnsafeMutablePointer<CGRect>, in view: AutoreleasingUnsafeMutablePointer<UIView>) {
if popoverPresentationController.presentingViewController is PopUpTextPickerViewController{
let viewFrame = popoverPresentationController.presentingViewController.view.frame
let newRect = CGRect(x: viewFrame.origin.x, y: viewFrame.origin.y, width: self.view.bounds.width, height: 100)
let newView = UIView(frame: newRect)
rect.pointee = newRect
view.pointee = newView
}
print("popoverPresentation:willRepositionPopOverTo")
}
//Shows the PopUpTextPickerViewController on the screen
@IBAction func fontButtonPressed(_ sender: UIBarButtonItem) {
subscribeToNotifications(notification: .popUpTextPickerViewController)
let fontController = storyboard?.instantiateViewController(withIdentifier: "popUpTextPickerViewController") as! PopUpTextPickerViewController
fontController.fontName = self.fontName
fontController.fontSize = self.fontSize
fontController.modalPresentationStyle = UIModalPresentationStyle.popover
fontController.popoverPresentationController?.delegate = self
fontController.popoverPresentationController?.barButtonItem = fontButton
fontController.popoverPresentationController?.backgroundColor = .clear
fontController.popoverPresentationController?.sourceView = self.view
fontController.preferredContentSize = CGSize(width: self.view.bounds.width, height: 100)
present(fontController, animated: true, completion: nil)
}
最佳答案
尝试在父 View Controller 中实现以下代码。 UIKit 在更改呈现的 View Controller View 的大小之前调用此方法 -
override func viewWillTransition(to size: CGSize,
with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
if let myVC = self.presentedViewController as? MyViewController {
myVC.preferredContentSize = CGSize(width: {yorWidth}, height: {yourHeight})
}
}
关于ios - 弹出演示 Controller (_ :willRepositionPopoverTo:in:) Trying to change the size of popUp on different orientation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46953862/