ios 如何更改弹出框的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37335147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change the size of a popover
提问by icestorm0806
I'm having trouble changing the size of my popover presentation. Here is what I have so far
我在更改 popover 演示文稿的大小时遇到问题。这是我到目前为止所拥有的
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) // func for popover
{
if segue.identifier == "popoverView"
{
let vc = segue.destinationViewController
let controller = vc.popoverPresentationController
if controller != nil
{
controller?.delegate = self
controller?.sourceView = self.view
controller?.sourceRect = CGRect(x:CGRectGetMidX(self.view.bounds), y: CGRectGetMidY(self.view.bounds),width: 315,height: 230)
controller?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
}
}
}
So far all this does is center the popover and remove the arrow, which is good. but it doesn't resize the container. any help would be greatly appreciated. thank you.
到目前为止,所有这些都是将弹出框居中并移除箭头,这很好。但它不会调整容器的大小。任何帮助将不胜感激。谢谢你。
when I use preferredContentSize I get the error "Cannot assign to property: 'preferredContentSize' is immutable"
当我使用 preferredContentSize 时出现错误“无法分配给属性:'preferredContentSize' 是不可变的”
回答by beyowulf
Set the preferred content size on the view controller being presented not the popoverPresentationController
在呈现的视图控制器上设置首选内容大小,而不是 popoverPresentationController
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) // func for popover
{
if segue.identifier == "popoverView"
{
let vc = segue.destinationViewController
vc.preferredContentSize = CGSize(width: 200, height: 300)
let controller = vc.popoverPresentationController
controller?.delegate = self
//you could set the following in your storyboard
controller?.sourceView = self.view
controller?.sourceRect = CGRect(x:CGRectGetMidX(self.view.bounds), y: CGRectGetMidY(self.view.bounds),width: 315,height: 230)
controller?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
}
}
回答by Xeieshan
回答by Kunsh Technologies
Above answers are correct which states about using the preferredContentSize
, but the most important thing is to implement the protocol UIPopoverPresentationControllerDelegate
and implement the below method otherwise it will not change the content size as expected.
上面的答案是正确的,其中说明了使用preferredContentSize
,但最重要的是实现协议UIPopoverPresentationControllerDelegate
并实现以下方法,否则它不会按预期更改内容大小。
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.none
}
回答by Ginés SM
I'm not using storyboards. I just present a UINavigationController in the popover:
我没有使用故事板。我只是在弹出窗口中呈现一个 UINavigationController :
self.present(popoverNavigationController!, animated: true) {}
The way to resize the popover size when a new view controller is pushed, it is just change the preferredContentSize before pushing it. For example:
推送新视图控制器时调整弹出框大小的方法,只需在推送之前更改 preferredContentSize。例如:
let newViewController = NewViewController()
popoverNavigationController!.preferredContentSize = CGSize(width: 348, height: 400)
popoverNavigationController!.pushViewController(newViewController, animated: true)
The problem is when we try to resize the popover when we pop a view controller.
问题是当我们在弹出视图控制器时尝试调整弹出框的大小。
If you use viewWillDisappear of the current view controller to change the preferredContentSize of the popover, the popover will resize but after the view controller is popped. That means that the animation has a delay.
如果您使用当前视图控制器的 viewWillDisappear 来更改 popover 的 preferredContentSize,则 popover 将调整大小,但在弹出视图控制器之后。这意味着动画有延迟。
You have to change the preferredContentSize before executing popViewController. That's mean you have to create a custom back button in the navigation bar like it is explained here. This is the implementation updated for Swift 4:
您必须在执行 popViewController 之前更改 preferredContentSize。这意味着您必须在导航栏中创建一个自定义后退按钮,就像这里解释的那样。这是为 Swift 4 更新的实现:
self.navigationItem.hidesBackButton = true
let newBackButton = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(CurrentViewController.backButtonTapped(sender:)))
self.navigationItem.leftBarButtonItem = newBackButton
And run the next code when the new Back button is pressed:
并在按下新的后退按钮时运行下一个代码:
@objc func backButtonTapped(sender: UIBarButtonItem) {
self.navigationController?.preferredContentSize = CGSize(width: 348, height: 200)
self.navigationController?.popViewController(animated: true)
}
Basically, the preferredContentSize has to be changed before pushing and popping the view controller.
基本上,在推送和弹出视图控制器之前必须更改 preferredContentSize。