我正在尝试制作类似于2014年WWDC第214期的弹出式窗口。
因此,我开始使用IB构建我的应用程序,该IB具有通过“Present As Popover” segue连接的两个 View ,如下所示:
弹出窗口 View 包含一个填充其 super View 的文本 View ,但具有以下约束:
为了支持模式弹出框,以下代码:
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return .OverFullScreen
}
func presentationController(controller: UIPresentationController, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? {
let navigationController = UINavigationController(rootViewController: controller.presentedViewController)
controller.presentedViewController.navigationItem.setRightBarButtonItem(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: "done"), animated: true)
return navigationController
}
这给了我以下结果:
然后,我尝试添加具有模糊效果的UIVisualEffectView。最初,我只是将 subview 添加到所显示的 Controller View 中,并且在检查了“ View 层次结构”之后,我意识到我的效果 View 实际上位于我的文本 View 之上,但是我的文本 View 已正确放置。所以我尝试了insertSubview:atIndex:这样的方法:
func presentationController(controller: UIPresentationController, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? {
let effectView = UIVisualEffectView(effect: UIBlurEffect(style: .Light)) as UIVisualEffectView
effectView.setTranslatesAutoresizingMaskIntoConstraints(false)
controller.presentedViewController.view.insertSubview(effectView, atIndex: 0)
let viewList : [String : UIView] = ["effectView" : effectView]
let navigationController = UINavigationController(rootViewController: controller.presentedViewController)
controller.presentedViewController.navigationItem.setRightBarButtonItem(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: "done"), animated: true)
controller.presentedViewController.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[effectView]|", options: nil, metrics: nil, views: viewList))
controller.presentedViewController.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[effectView]|", options: nil, metrics: nil, views: viewList))
return navigationController
}
但是我仍然无法呈现IB中定义的文本 View :
通过“ View 层次结构”,我可以看到,当我在索引0处插入UIVisualEffectView时,我的文本 View 正以某种方式被移动到导航栏下方。
关于如何以编程方式在文本下方设置模糊效果而不会使其在导航栏下方消失/移动的任何想法?
干杯
最佳答案
此功能对您有帮助吗?
view.insertSubview(view: effectView, belowSubview: label)