添加自定义弹出窗口不会填满整个视图 iOS、Swift
Adding Custom popup doesn't filling the full view iOS, Swift
我创建了一个 UIViewControlller 并在情节提要中进行了设计,我想将其显示为另一个视图的弹出窗口。
我使用以下内容显示为弹出窗口:
guard let popupVC = UIStoryboard(name: "More", bundle: nil).instantiateViewController(withIdentifier: "LanguageSelectionViewController") as? LanguageSelectionViewController else{
fatalError("Unexpected destination VC")
}
self.addChildViewController(popupVC)
popupVC.view.frame = self.tableView.frame
print("POPUP Frame VIEW -- \(popupVC.view.frame)")
print("SElF VIEW -- \(self.tableView.frame)")
popupVC.view.frame = self.tableView.frame
print("POPUP Frame VIEW -- \(popupVC.view.frame)")
print("SElF VIEW -- \(self.view.frame)")
popupVC.modalPresentationStyle = .currentContext
popupVC.modalTransitionStyle = .crossDissolve
popupVC.view.bindFrameToSuperviewBounds()
self.view.addSubview(popupVC.view)
popupVC.didMove(toParentViewController: self)
它工作正常,但在我的设备上一半屏幕 space 没有被弹出视图填充。
首先你需要点击弹出窗口(故事板或 xib)并将其设置为背景色 .clear Color
然后让它居中。像
popupVC.center = self.view.center
这只是一个指南,您可以相应地更改和设置名称。但我的问题是你为什么使用 storyboard
为什么不创建 Xib or View
希望它能为您服务。
您尝试过使用 present() 函数吗?像这样:
let popupVC = PopupViewController()
popupVC.modalPresentationStyle = .overFullScreen
popupVC.modalTransitionStyle = .crossDissolve
self.present(popupVC, animated: true, completion: nil)
我发现在自己的 xib 文件中设计视图控制器更容易,并且它允许像我的示例中那样进行初始化,而不必在代码中使用故事板,但这是个人喜好。
如果它仍然不起作用,也许检查弹出窗口背景的布局约束是否正确,所以它实际上填充了它的父级。
你需要交换这两行:
self.view.addSubview(popupVC.view)
popupVC.view.bindFrameToSuperviewBounds()
如果视图尚未添加到它,则不能将视图绑定到它的父视图。
我创建了一个 UIViewControlller 并在情节提要中进行了设计,我想将其显示为另一个视图的弹出窗口。 我使用以下内容显示为弹出窗口:
guard let popupVC = UIStoryboard(name: "More", bundle: nil).instantiateViewController(withIdentifier: "LanguageSelectionViewController") as? LanguageSelectionViewController else{
fatalError("Unexpected destination VC")
}
self.addChildViewController(popupVC)
popupVC.view.frame = self.tableView.frame
print("POPUP Frame VIEW -- \(popupVC.view.frame)")
print("SElF VIEW -- \(self.tableView.frame)")
popupVC.view.frame = self.tableView.frame
print("POPUP Frame VIEW -- \(popupVC.view.frame)")
print("SElF VIEW -- \(self.view.frame)")
popupVC.modalPresentationStyle = .currentContext
popupVC.modalTransitionStyle = .crossDissolve
popupVC.view.bindFrameToSuperviewBounds()
self.view.addSubview(popupVC.view)
popupVC.didMove(toParentViewController: self)
它工作正常,但在我的设备上一半屏幕 space 没有被弹出视图填充。
首先你需要点击弹出窗口(故事板或 xib)并将其设置为背景色 .clear Color
然后让它居中。像
popupVC.center = self.view.center
这只是一个指南,您可以相应地更改和设置名称。但我的问题是你为什么使用 storyboard
为什么不创建 Xib or View
希望它能为您服务。
您尝试过使用 present() 函数吗?像这样:
let popupVC = PopupViewController()
popupVC.modalPresentationStyle = .overFullScreen
popupVC.modalTransitionStyle = .crossDissolve
self.present(popupVC, animated: true, completion: nil)
我发现在自己的 xib 文件中设计视图控制器更容易,并且它允许像我的示例中那样进行初始化,而不必在代码中使用故事板,但这是个人喜好。 如果它仍然不起作用,也许检查弹出窗口背景的布局约束是否正确,所以它实际上填充了它的父级。
你需要交换这两行:
self.view.addSubview(popupVC.view)
popupVC.view.bindFrameToSuperviewBounds()
如果视图尚未添加到它,则不能将视图绑定到它的父视图。