【问题标题】:Popover presentation style on iPhone devices - possible any more?iPhone 设备上的 Popover 演示样式 - 还可以吗?
【发布时间】:2017-07-24 16:08:38
【问题描述】:

我正在尝试定义一个附加到这样的视图的弹出视图:

这是我的代码:

class MyController: UIViewController, UIPopoverPresentationControllerDelegate {

    ...

    func displaySignOut(_ sender: UIButton) {
        let vc = UIStoryboard(name: "Main", bundle: nil)
            .instantiateViewController(withIdentifier: "signOutPopover")
        vc.modalPresentationStyle = .popover
        vc.preferredContentSize = CGSize(width: 100, height: 30)
        present(vc, animated: true, completion: nil)

        let pc = vc.popoverPresentationController!
        pc.sourceView = sender
        pc.sourceRect = sender.bounds
        pc.delegate = self
    }

    func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
        return .none
    }

    func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
        return .none
    }
}

因为弹出框很小,我想在所有设备上都使用这种样式。我遵循了关于覆盖adaptivePresentationStyle 以返回UIModalPresentationStyle.none 的常规建议(例如here)。

这在 iPad 设备上运行良好,但在 iPhone 上却不行。在较小的 iPhone 设备上,它总是全屏显示。在较大的屏幕(例如,iPhone 7 Plus)上,它会出现错误,但奇怪的是,如果我在弹出框出现后旋转设备,则会切换到弹出框演示(纵向和横向)。 (如果我关闭弹出框并再次打开它,在我旋转设备之前它又是错误的。)此外,在横向它会以一种奇怪的配置出现(不像纵向那样全屏):

与弹出式演示不同,如果我在弹出式视图本身之外点击,它不会关闭。

Apple documentation 说(部分):

在水平紧凑的环境中,弹出框默认适应UIModalPresentationOverFullScreen 呈现风格。

“默认”强烈暗示有一种方法可以覆盖此行为。但是(与this post 一致),在委托中覆盖adaptivePresentationStyle 似乎不再是这样做的方法(尽管它曾经有效)。那么有没有新的方法来修改默认行为呢?

我正在使用 XCode 8.3.3 和 Swift 3.1,针对 iOS 9+。

【问题讨论】:

    标签: ios iphone swift3 popover


    【解决方案1】:

    iOS 15 有一些新方法可以解决这个问题。 看看 WWDC21 会议“在 UIKit 中自定义和调整表格大小”https://developer.apple.com/wwdc21/10063 弹出框和自定义工作表的非常简单的新界面。展示如何使用 pop over 及其背后的视图进行非模态交互。

    【讨论】:

    • 听起来是一个很大的改进。我来看看材料。谢谢!
    【解决方案2】:

    我创建了一个自定义类,其中包含连接的情节提要 按钮的出口并在下面的代码中实现。

    import UIKit
    class PopOverViewController: UIViewController {
    
        @IBOutlet weak var button: UIButton!
        override func viewDidLoad() {
            super.viewDidLoad()
            button.backgroundColor = UIColor.purple
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        //Updating the popover size
        override var preferredContentSize: CGSize {
            get {
                let size = CGSize(width: 80, height: 60)
                return size
            }
            set {
                super.preferredContentSize = newValue
            }
        }
    
    
        //Setup the ViewController for popover presentation
        func updatePopOverViewController(_ button: UIButton?, with delegate: AnyObject?) {
            guard let button = button else { return }
            modalPresentationStyle = .popover
            popoverPresentationController?.permittedArrowDirections = [.any]
            popoverPresentationController?.backgroundColor = UIColor.purple
            popoverPresentationController?.sourceView = button
            popoverPresentationController?.sourceRect = button.bounds
            popoverPresentationController?.delegate = delegate
        }
    
    }
    

    然后在ViewController里面实现了一个函数来展示 iphone上的popOver

    func showPopOver(button: UIButton!) {
     let viewController = PopOverViewController()
     viewController.updatePopOverViewController(button, with: self)
      present(viewController, animated: true, completion: nil)
    }
    

    注意:- 经过测试,这应该适用于纵向以及横向模式

    【讨论】:

    • 这很好用。我承认,当我第一次看到它时,我认为它只是对我尝试过的相同代码的重组,并且它的工作方式没有什么不同。但显然情况并非如此。你能解释一下为什么当我的代码不起作用时它会起作用吗?
    • 我可以在您设置框架但未更新 super.preferredContentSize 中的值的首选内容大小上看到问题。所以在我的代码中,我还设置了 super.preferredContentSize = newValue。这可能是你案件的原因。这有意义吗?
    • 我知道这可能是一个影响大小的问题。但在很多情况下,我的版本甚至没有提供popover 样式。错误的不仅仅是尺寸。我想了解为什么我的版本失败而您的版本运行良好。设置大小(或不这样做)会导致整个样式发生变化吗?
    • 我明白你的意思,但到目前为止我只能看到设置大小的差异。首选内容大小。除此之外,我没有看到您的代码有任何问题。
    • 很奇怪。也许我会多尝试一下,以期获得更好的洞察力。无论如何,你解决了我的问题,非常感谢!
    猜你喜欢
    • 2017-05-13
    • 1970-01-01
    • 2011-10-07
    • 1970-01-01
    • 2011-06-26
    • 1970-01-01
    • 2012-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多