这与iOS 11.4、Swift 4和Xcode 9有关。
我试图在当前上下文上显示一个视图控制器,这样底层的上下文就会变暗,但仍然是可见的。我这样做的代码如下:
let storyboard = UIStoryboard(name: "Main", bundle: nil)l
let vc = storyboard.instantiateViewController(withIdentifier: "LoginView") as! LoginViewController
vc.modalPresentationStyle = .overCurrentContext
vc.modalTransitionStyle = .coverVertical
present(vc, animated: true, completion: nil)
let ppc = vc.popoverPresentationController
ppc?.sourceView = tableView
ppc?.sourceRect = tableView.bounds
ppc?.delegate = vc
LoginViewController有一个以它的mainview为中心的视图(popview),其中包含我的可编辑控件。这在一开始很好,但我遇到了一些问题:
我试图通过为LoginViewController的主视图使用passtrough视图来修复问题1
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView?
{
let view = super.hitTest(point, with: event)
return view == self ? nil : view
}
这没什么用。我可以在主视图上使用一个UITapGestureRecognizer,如果触控不是流行视图,我可以从那里调用dismiss,但对我来说,这似乎相当笨拙,我想知道是否有更好的方法。
我不知道如何解决问题2,我尝试了UIPopoverPresentationController的委托函数,但这也不起作用,因为当我使用.overCurrentContext时,vc.popoverPresentationController总是为零。
当我将modalPresentationStyle设置为.popover时,我会为popoverPresentationController获得一个值,但超过我的LoginView将完全涵盖呈现控制器视图。
发布于 2018-07-29 05:07:01
经过进一步的挖掘,我最终找到了一个解决问题2的方法,用Android的方式:
在呈现的视图控制器中,我添加了以下函数:
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?)
{
if (loginViewController != nil)
{
present(loginViewController!, animated:false, completion: nil)
}
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)
{
super.viewWillTransition(to: size, with: coordinator)
loginViewController!.dismiss(animated: false, completion: nil)
}
因此,当一个方向更改传入时,popover视图首先被取消,并且在执行更改之后,弹出再次出现。由于呈现的视图控制器在此过程中没有受到损害,即使已经输入到呈现视图控制器的控件中的数据也仍然存在。
为了解决问题1,我在呈现的视图控制器中添加了以下内容:
override func viewDidLoad()
{
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(sender:)))
view.addGestureRecognizer(tapGesture)
}
和
@objc func handleTap(sender: UITapGestureRecognizer)
{
let touchpoint = sender.location(in: view)
let popviewrect = popview.frame
if (!popviewrect.contains(touchpoint))
{
dismiss(animated: true, completion: nil)
delegate?.onDismissed()
}
}
呈现控制器实现onDismissed委托:
func onDismissed()
{
loginViewController = nil
}
仍然想知道是否有更优雅的方法来做到这一点
https://stackoverflow.com/questions/51579412
复制相似问题