ios - 弹出演示 Controller (_ :willRepositionPopoverTo:in:) Trying to change the size of popUp on different orientation

标签 ios swift uipopovercontroller

我有一个弹出窗口,我试图在方向改变时改变宽度。当弹出窗口以当前方向显示时,我得到了我想要的宽度,但是当我从 .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/

相关文章:

ios - Interface Builder 中的 WKWebView

ios - 除非选择为目标,否则框架中的 Xcode 警告不会显示在项目导航器中

IOS开发:Tab Bar Item's Image Display a Blue Square

Swift,此类不符合键 "array"的键值编码

ios - 在 UIPopoverController 中自定义 titleView

ios - AWS SDK iOS - 如何确定上传请求的完成百分比

ios - 如何让 iOS 缓存带有查询参数的 HTML5 文档?

iOS 模拟器没有给我 Game Center 的沙盒选项

ios - 重置 UITabBarController 上的导航堆栈不起作用

类似于苹果商店的ios过滤选项(下拉列表)