ios 从 UITabBarController 在当前上下文中呈现模态视图控制器后的黑屏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27785467/
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
Black screen after presenting modal view controller in current context from UITabBarController
提问by alivingston
My root view controller is a UITabBarController. I'm trying to present a modal view controller over one of the tab bar controller's view controllers, but still allow the tab bar to be used to go to a different tab - in other words, I would like for the modal to only interrupt to the flow of that particular tab, not the entire app.
我的根视图控制器是一个 UITabBarController。我正在尝试在选项卡栏控制器的视图控制器之一上显示模态视图控制器,但仍然允许使用选项卡栏转到不同的选项卡 - 换句话说,我希望模态仅中断该特定选项卡的流程,而不是整个应用程序。
To do this, I've set the presenting view controller's presentation style as 'Over Current Context' in storyboard. The problem I'm having is that after presenting the modal view controller and selecting a new tab, the presenting view controller's view is removed from the window, and isn't added back to the window when dismissing the presented view controller. After dismissing the view controller, moving to another tab and then coming back finally puts the presenting view controller back into the window.
为此,我在故事板中将呈现视图控制器的呈现样式设置为“Over Current Context”。我遇到的问题是,在呈现模式视图控制器并选择一个新选项卡后,呈现视图控制器的视图将从窗口中删除,并且在关闭呈现的视图控制器时不会添加回窗口。关闭视图控制器后,移动到另一个选项卡然后返回最终将呈现视图控制器放回窗口中。
I've reproduced my problem using the 'Tabbed' template in Xcode.
我使用 Xcode 中的“Tabbed”模板重现了我的问题。
After presenting modal - I've added transparency to the presented view controller to easily see what's going on in the presented view controller.
在呈现模态之后 - 我已经为呈现的视图控制器添加了透明度,以便轻松查看呈现的视图控制器中发生的事情。
Changing to second tab and then back - the presenting view controller's view has now been removed.
更改为第二个选项卡然后返回 - 呈现视图控制器的视图现在已被删除。
Dismissing the modal leaves the presenting view controller with it's view still removed from the window. Going to tab 2 and returning adds the view back to the window.
关闭模态会使呈现视图控制器的视图仍然从窗口中移除。转到选项卡 2 并返回将视图添加回窗口。
I'm hoping this is something simple I've overlooked in storyboard, but the fact that I can present the modal and see the presented view controller behind it before changing tabs makes me think I have things set up correctly.
我希望这是我在故事板中忽略的一些简单的事情,但事实上我可以在更改选项卡之前展示模态并查看其背后呈现的视图控制器,这让我认为我已经正确设置了一些东西。
采纳答案by Ole Begemann
I had the same issue and was able to solve it by setting self.definesPresentationContext = YES;
on the presenting view controller before presenting the modal VC. You can also set this in a storyboard, the checkbox is called "Defines Context" in Interface Builder.
我遇到了同样的问题,并且能够通过self.definesPresentationContext = YES;
在呈现模态 VC 之前在呈现视图控制器上进行设置来解决它。您也可以在故事板中设置它,该复选框在界面生成器中称为“定义上下文”。
回答by malajisi
回答by malajisi
iOS 10+ & Swift 3+?
iOS 10+ 和 Swift 3+?
I've very nice solution of this problem. Use, over full screen modal presentation style for a view controller, is being presented.
我对这个问题有很好的解决方案。正在呈现视图控制器的全屏模式呈现样式的使用。
let storyboard = UIStoryboard(name: "Main", bundle: nil) // Replace “Main” with your storyboard name
if let viewController = storyboard?.instantiateViewController(withIdentifier: “viewController Identifier”) as? ViewController {
viewController.modalPresentationStyle = .overFullScreen
self.present(viewController, animated: false, completion: {
})
}
Over full screen will present your view controller over your tabbar (controller) by covering it. So, end-user can't switch tabbar (unless you perform operation programatically) tabbar items. User must close this view controller to switch tabbar.
全屏显示将您的视图控制器覆盖在您的标签栏(控制器)上。因此,最终用户无法切换标签栏(除非您以编程方式执行操作)标签栏项目。用户必须关闭此视图控制器才能切换标签栏。
If you are using segue, to present view controller then select 'Over Full Screen' from modal presentation style in Attribute inspection
如果您正在使用 segue,要呈现视图控制器,请从属性检查中的模态呈现样式中选择“全屏显示”
回答by jamal zare
i had this problem : when i present my ModalController it takes transparent background and when i changed the tab to next one in tabBarController and getBack to previous one the transparent background was gone and and there is a bad black background after research i found the point the point is this:
我遇到了这个问题:当我展示我的 ModalController 时,它需要透明背景,当我将选项卡更改为 tabBarController 中的下一个并返回上一个时,透明背景消失了,并且研究后黑色背景很糟糕,我发现了这一点重点是:
self.definesPresentationContext = true
self.definesPresentationContext = true
the self is not modal controller self is that presenting controller for modalController and another point is .overCurrentContext like this
self 不是模态控制器 self 是为 modalController 呈现控制器,另一点是像这样的 .overCurrentContext
** self.definesPresentationContext = true **
modalController.modalPresentationStyle = .overCurrentContext
self.present(modalController, animated: true, completion: nil)
** self.definesPresentationContext = true **
modalController.modalPresentationStyle = .overCurrentContext
self.present(modalController, animated: true, completion: nil)
回答by Shobhit C
Try presenting the view controller in application window. I had a similar problem which was fixed by below code:
尝试在应用程序窗口中呈现视图控制器。我有一个类似的问题,已通过以下代码修复:
let myNewVC = mainStoryBoard.instantiateViewController(withIdentifier: "MyNewVCId") as! MyNewVC
let navController = UINavigationController(rootViewController: myNewVC)
navController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
let appDelegate = UIApplication.shared.delegate as? AppDelegate
appDelegate?.window?.rootViewController?.present(navController, animated: true, completion: nil)
Hope this helps you too.
希望这对你也有帮助。
回答by saugat gautam
I reproduced your problem and found a solution. It does not involve changing the segue method or changing some attributes in the storyboard.
我重现了您的问题并找到了解决方案。它不涉及更改 segue 方法或更改故事板中的某些属性。
Suggestion:
建议:
但在进入解决方案之前,我想补充一点,模态呈现的视图控制器的目的是扰乱应用程序的实际流程,并为呈现的 vc 呈现一些额外的上下文信息或一些可操作的内容。简单地说,这是非常合乎逻辑的,实际上建议在以模态方式呈现视图控制器时覆盖选项卡栏。在应用商店中有很多很好的例子。Having said that here is a solution I propose.
话虽如此,这是我提出的解决方案。
Solution:
解决方案:
我相信问题在于 UITabBarController 处理其视图层次结构的方式。我所做的是在更改选项卡之前明确关闭模态呈现的视图控制器。这使得呈现视图控制器在选项卡栏切换到新选项卡之前一直保留在 UITabBarViewController 的视图层次结构中。In the "viewWillDisappear" method of your modally presented ViewController add this.
在模态呈现的 ViewController 的“viewWillDisappear”方法中添加这个。
- (void)viewWillDisappear:(BOOL)animated {
[self dismissViewControllerAnimated:true completion:^{
[super viewWillDisappear:animated];
}];
}
回答by Will Von Ullrich
Having a similiar issue with my tab controller, however like the comments, I suggest changing the segue to a push or a show segue. This will allow that tab to remain in tact with the new view shown in place of the old view when switching to other tabs. If aesthetics is an issue you can make a custom navigation controller to customize the appearance of the new view.
我的标签控制器有一个类似的问题,但是就像评论一样,我建议将转场更改为推送或显示转场。这将允许该选项卡在切换到其他选项卡时与代替旧视图显示的新视图保持一致。如果美学是一个问题,您可以制作一个自定义导航控制器来自定义新视图的外观。
回答by Max
I have same problem in currently live swift project. I have did workaround on that.
我在当前的 swift 项目中遇到了同样的问题。我已经解决了这个问题。
Then finally i have used NSNotificationCenter
and dismiss that view controller when tab is changed to solve this problem.
然后最后我在NSNotificationCenter
更改选项卡以解决此问题时使用并关闭了该视图控制器。
I have referenced tabbar controller in AppDelegate
and set delegate there.
我已经在其中引用了 tabbar 控制器AppDelegate
并在那里设置了委托。
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let tabbarcontroller = storyboard.instantiateViewControllerWithIdentifier("MyTabBarController") as! UITabBarController
tabbarcontroller.delegate = self
And it's delegate as
它是委托为
//MARK: - Tabbar controller delegate
extension AppDelegate : UITabBarControllerDelegate {
func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
NSNotificationCenter.defaultCenter().postNotificationName("TabBarTabChanged", object: nil)
}
}
Then I have add observer in my presented view controller as
然后我在我呈现的视图控制器中添加了观察者作为
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(PreviewPlaceVC.BackClickAction(_:)), name: "TabBarTabChanged", object: nil)
}
// MARK: - Button Click Actions
@IBAction func BackClickAction(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
And it's work fine for me. Not a proper solution I think to dismiss view controller on tab change event, but it is ok to dismiss rather than black screen which also breaks navigation that time.
它对我来说很好用。我认为在选项卡更改事件上关闭视图控制器不是一个合适的解决方案,但是可以关闭而不是黑屏,这也会在那时破坏导航。
回答by Ing. Ron
I also needed a Navigation Controllerbefore the calling ViewController!
在调用 ViewController 之前,我还需要一个导航控制器!
And I used the described code before:
我之前使用了描述的代码:
let newVC = self.storyboard?.instantiateViewController(withIdentifier: "newViewController")
self.definesPresentationContext = true
self.providesPresentationContextTransitionStyle = true
newVC?.modalPresentationStyle = .overCurrentContext //do NOT use .currentContext --> it will produce a black screen by switching over tabbar and close VC
self.present(newVC!, animated: false)