[英]UIPopoverPresentationController displaying popover as full screen
I am trying to use UIPopoverPresentationController
to display a popover
that doesn't take up the whole screen.我正在尝试使用UIPopoverPresentationController
来显示不占据整个屏幕的popover
窗口。 I've followed many different tutorials with no luck.我遵循了许多不同的教程,但没有运气。
Here is my code.这是我的代码。 It correctly instantiates the ViewController
, but it takes up the entire screen instead of just a smaller screen as I defined in preferredContentSize
.它正确地实例化了ViewController
,但它占据了整个屏幕,而不是我在preferredContentSize
定义的一个较小的屏幕。
func showPopover() {
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("PopupTimePickerViewController") as PopupTimePickerViewController
vc.modalPresentationStyle = .Popover
vc.preferredContentSize = CGSizeMake(200, 100)
if let presentationController = vc.popoverPresentationController {
presentationController.delegate = self
presentationController.permittedArrowDirections = .Up
presentationController.sourceView = self.view
presentationController.sourceRect = CGRectMake(0, 0, 50, 50)
self.presentViewController(vc, animated: true, completion: nil)
}
}
Update 9/27/16 with correct answer更新 9/27/16 并提供正确答案
func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
return .none
}
In iPhone, you should add the following in order to present a popover.在 iPhone 中,您应该添加以下内容以呈现弹出窗口。
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController!) -> UIModalPresentationStyle {
// Return no adaptive presentation style, use default presentation behaviour
return .None
}
For Swift3/IOS10, looks like we need to do some thing like对于 Swift3/IOS10,看起来我们需要做一些类似的事情
func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle
{
return .none
}
Adding this answer, in case, someone runs into this problem as i did when migrating to swift3/IOS10添加这个答案,以防万一有人遇到这个问题,就像我在迁移到 swift3/IOS10 时所做的那样
For Swift3+/IOS10+, when dealing with iPhone:对于 Swift3+/IOS10+,处理 iPhone 时:
You must add UIPopoverPresentationControllerDelegate the delegate at:您必须在以下位置添加UIPopoverPresentationControllerDelegate委托:
class YourClass: UIViewController, UIPopoverPresentationControllerDelegate { ...
Then implement in this same parent class (which will show the popover) the method below.然后在同一个父类(将显示弹出窗口)中实现下面的方法。
func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle
{
return .none
}
And then set the popover configuration below:然后在下面设置popover配置:
myPopover.modalPresentationStyle = .popover
myPopover.popoverPresentationController?.sourceRect = VIEWTOPOINTTHEARROW.frame
myPopover.popoverPresentationController?.sourceView = self.view
myPopover.popoverPresentationController?.delegate = self
Also you may set some configuration for the popover class您也可以为 popover 类设置一些配置
class MyPopover: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//popover size
self.preferredContentSize = CGSize(width: 320, height: 200)
//sets the arrow of the popover to same color of background
self.popoverPresentationController?.backgroundColor = self.view.backgroundColor
}
}
The accepted answer is correct.接受的答案是正确的。 For completeness, see Adapting Presented View Controllers to a New Style in the Apple docs:为了完整起见,请参阅 Apple 文档中的Adapting Presented View Controllers to a New Style :
Use the delegate's
adaptivePresentationStyleForPresentationController:
method to specify a different presentation style than the default.使用委托的adaptivePresentationStyleForPresentationController:
方法来指定与默认不同的呈现样式。 When transitioning to a compact environment, the only supported styles are the two full-screen styles orUIModalPresentationNone
.转换到紧凑环境时,唯一支持的样式是两种全屏样式或UIModalPresentationNone
。 ReturningUIModalPresentationNone
tells the presentation controller to ignore the compact environment and continue using the previous presentation style.返回UIModalPresentationNone
告诉呈现控制器忽略紧凑环境并继续使用以前的呈现样式。 In the case of a popover, ignoring the change gives you the same iPad-like popover behavior on all devices.在弹出窗口的情况下,忽略更改会在所有设备上为您提供类似 iPad 的弹出窗口行为。
Make sure that the required configurations from Presenting a View Controller in a Popover are met:确保满足在 Popover中呈现视图控制器所需的配置:
After setting the modal presentation style [of the presented view controller] to
UIModalPresentationPopover
, configure the following popover-related attributes:将[被呈现的视图控制器的]模态呈现样式设置为UIModalPresentationPopover
,配置以下UIModalPresentationPopover
相关的属性:
- Set the
preferredContentSize
property of your view controller to the desired size.将您的视图控制器的preferredContentSize
属性设置为所需的大小。- Set the popover anchor point using the associated
UIPopoverPresentationController
object, which is accessible from the view controller'spopoverPresentationController
property.使用关联的UIPopoverPresentationController
对象设置弹出UIPopoverPresentationController
,该对象可从视图控制器的popoverPresentationController
属性访问。- Set only one of the following:仅设置以下其中一项:
- Set the
barButtonItem
property to a bar button item.将barButtonItem
属性设置为条形按钮项。- Set the
sourceView
andsourceRect
properties to a specific region in one of your views.在您的视图之一中将sourceView
和sourceRect
属性设置为特定区域。
There's also the possibility to show the popover on IPhone's as fullscreen and on IPad's as a popover.还可以在 iPhone 上以全屏显示弹出框,在 iPad 上将弹出框显示为弹出框。
Just return .popover
for adaptivePresentationStyle()
:只需为adaptivePresentationStyle()
返回.popover
:
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .popover
}
and set the popover-configuration like @mourodrigo did:并像@mourodrigo 那样设置 popover-configuration:
dialog.modalPresentationStyle = .popover
dialog.popoverPresentationController?.delegate = self
dialog.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
dialog.popoverPresentationController?.sourceView = view
dialog.popoverPresentationController?.sourceRect = view.frame
objective-c version:目标 C 版本:
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
return UIModalPresentationNone;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.