nm1213
nm1213

Reputation: 99

View controller goes black after returning from another tab

I get a black screen when returning to a view controller from a different tab.

I have the following flow:

  1. A tab bar with 5 tabs.
  2. A viewcontroller called Points, which contains a button called “Rewards” that segues to another viewcontroller called Rewards. a. There is also a button called “Points” that does nothing. b. The Points viewcontroller is set to “Define Context” c. The segue is set to present modally, and the presentation is set to Current Context. d. There is also a segue function called backFromRewards, which is used to unwind.
  3. The Rewards viewcontroller also has a button called “Points”, and this button is set to unwind back to the Points viewcontroller via the segue function called backFromRewards.

The goal for the above setup was to always have the tab bar presented even though the rewards viewcontroller was presented modally. This works so far. The tab bar is indeed always visible, and I can alternate between the Points and Rewards viewcontrollers by pressing the Points and Rewards buttons respectively. I can also tab to other tabs as needed.

However, I get a black screen when I do the following:

  1. From the Points viewcontroller, I select the Rewards button. This brings up the Rewards viewcontroller.
  2. I then tab to a different tab.
  3. I come back to the original tab. The rewards viewcontroller is still displayed.
  4. I then click the “Points” button, which is set to unwind back to the points viewcontroller.
  5. This is when I get a black screen.
  6. I can then tab somewhere else, and then tab back to the points viewcontroller and it refreshes back to normal, getting rid of the black screen.

I somewhat understand why the screen goes black. However, is there a way to reload and reset an entire viewcontroller when a tab button is pressed? Perhaps this will get rid of the black screen. Otherwise, is there any other way, other than by presenting modally to let the tab bar disappear?

Upvotes: 0

Views: 370

Answers (2)

Milan
Milan

Reputation: 141

Try set the modalPresentationStyle of the presented controller i.e., Rewards ViewController by going to Attributes Inspector

enter image description here

Upvotes: 0

nm1213
nm1213

Reputation: 99

Alright, I think I have this figured out.

I added this to my Rewards view controller:

override func viewWillDisappear(_ animated: Bool) {
    self.dismiss(animated: true, completion: nil)
    super.viewWillAppear(true)
}

This resets the view controller as follows:

  1. From the Points View Controller, I click the Rewards button, and it takes me to the Rewards View Controller.
  2. I then go to another tab, and then tab back to the Points View Controller.
  3. Now, this reloads the Points View Controller.

Upvotes: 0

Related Questions