简体   繁体   English

呈现一个没有 segue 的 popover -- Popover 仍然是全屏的

[英]Presenting a popover without segue -- Popover is still full screen

I have a UIViewController with a button in it that I made programmatically (Let's just call it MyViewController).我有一个 UIViewController,里面有一个我以编程方式制作的按钮(让我们称之为 MyViewController)。 I am trying to present a popover when the button is tapped.我试图在点击按钮时呈现一个弹出框。 Let's call the popover DestinationViewController.让我们调用弹出窗口 DestinationViewController。 The button will call the following function when tapped:该按钮在点击时将调用以下函数:

func buttonAction(_ button: UIView) {
   var popover = storyboard?.instantiateViewController(withIdentifier: "destinationVC") as? DestinationViewController
   popover?.popoverPresentationController?.delegate = self
   popover?.modalPresentationStyle = .popover
   popover?.popoverPresentationController?.sourceView = button
   //popover?.popoverPresentationController?.sourceRect = CGRect(x: 0, y: 0, width: 50, height: 50)
   popover?.preferredContentSize = CGSize(width: 200, height: 100)

   present(popover!, animated: true, completion: nil)         
}

(I commented out the sourceRect line because it wasn't doing anything but I don't know if I'm just using it wrong.) (我注释掉了 sourceRect 行,因为它没有做任何事情,但我不知道我是否只是使用错误。)

Additionally, I made sure that MyViewController conforms to the protocol UIPopoverPresentationControllerDelegate.此外,我确保 MyViewController 符合 UIPopoverPresentationControllerDelegate 协议。 So MyViewController implements the function:于是 MyViewController 实现了这个功能:

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

Finally, in my storyboard for the DestinationViewController, I have checked the box "Use Preferred Explicit Size" with 200 width and 100 height.最后,在 DestinationViewController 的故事板中,我选中了“使用首选显式大小”框,宽度为 200,高度为 100。

However, my DestinationViewController is still being displayed full screen.但是,我的 DestinationViewController 仍然全屏显示。 I have read that the adaptivePresentationStyle function is supposed to prevent this, but no luck.我已经读过,adaptivePresentationStyle 函数应该可以防止这种情况发生,但没有运气。 I know that using a segue will make it work correctly, but I can't make a segue in storyboard because my button was made programmatically.我知道使用 segue 会使其正常工作,但我无法在故事板中进行 segue,因为我的按钮是通过编程方式制作的。

Any ideas on what I should do to fix this?关于我应该怎么做来解决这个问题的任何想法? I'm not that familiar with making popovers so I need a bit of help.我不太熟悉制作爆米花,所以我需要一些帮助。

You need to set the modalPresentationStyle to .popover before you access the popoverPresentAtionController property:您需要将设置modalPresentationStyle.popover您访问之前popoverPresentAtionController属性:

From the documentation :文档

If you created the view controller but have not yet presented it, accessing this property creates a popover presentation controller when the value in the modalPresentationStyle property is UIModalPresentationStyle.popover.如果您创建了视图控制器但尚未呈现它,当 modalPresentationStyle 属性中的值为 UIModalPresentationStyle.popover 时,访问此属性会创建一个弹出式呈现控制器。 If the modal presentation style is a different value, this property is nil.如果模态呈现样式是不同的值,则此属性为零。

As you haven't set the modal presentation style before you set the delegate, no popover presentation controller is created.由于您在设置委托之前没有设置模态展示样式,因此不会创建弹出式展示控制器。 The popover controller will be created when you set the .sourceView , as the presentation style is now .popover but the delegate won't be set on this instance.当您设置.sourceView ,将创建.sourceView ,因为演示样式现在是.popover但不会在此实例上设置委托。

Also, you can use a segue with a button you create in code by using performSegue in the buttons action handler code.此外,您可以通过在按钮操作处理程序代码中使用performSegue来将 segue 与您在代码中创建的按钮一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM