2017-02-21 58 views
0
let obj = MainStoryboard().instantiateViewController(withIdentifier: "SomeVC") as! SomeVC 
obj.delegate = self 

obj.modalPresentationStyle = .popover 
obj.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0) 
self.present(obj, animated: true, completion: nil) 

关于设置断点,调试器进入最后一行。之后,它直接进入AppDelegate班的第一线。Swift3:在弹出时出现崩溃

我已经正确设置了异常中断点。我可能在哪里犯错误?是否与sourceView有关popoverPresentationController?我不确定。

我想要做的是在中心设置popoverPresentationController。任何帮助?

编辑: 我加入了sourceView的代码像下面&现在它的工作:

obj.popoverPresentationController?.sourceView = self.view 
obj.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 1, height: 1) 

然而,这是不是在屏幕的中央。把屏幕截图作为参考。我如何将它放到中心并移除方向箭头?

enter image description here

+1

任何崩溃报告? – raki

+0

在控制台中,没有。除了:'libC++ abi.dylib:终止于类型为NSException的未捕获异常' –

+1

显示完整的崩溃报告 –

回答

1

你必须结合使用sourceViewsourceRect超过规定流行锚点,像以下:

obj.popoverPresentationController?.sourceView = self.view 
obj.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 1, height: 1) 

另外,如果你不想锚点箭头到那里,然后用:

obj.popoverPresentationController?.permittedArrowDirections = .init(rawValue: 0) 

它会让你的弹出显示在没有箭头/定位点的中央。

1
Try this: 
    let obj = MainStoryboard().instantiateViewController(withIdentifier: "SomeVC") as! SomeVC 
    obj.delegate = self 
    obj.modalPresentationStyle = .overCurrentContext 
    self.navigationController?.present(obj, animated: false, completion: { 
          }) 
+0

这不是崩溃,但弹出窗口是全屏,我希望它是682 x 450的大小。所以,有点不起作用。 –

+0

我不知道这是否可能。尝试更改objViewcontroller的约束,以便将其呈现为所需的维度。 – Anuraj

2

完成以下代码更改后,我才能使其工作。

let obj = MainStoryboard().instantiateViewController(withIdentifier: "SomeVC") as! SomeVC 
obj.delegate = self 

obj.modalPresentationStyle = .popover 
obj.popoverPresentationController?.permittedArrowDirections = .init(rawValue: 0) 
obj.popoverPresentationController?.sourceView = self.view 
obj.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 1, height: 1) 
self.present(obj, animated: true, completion: nil) 

谢谢大家的努力。