Swift:通过Segue返回ViewController后Tab Bar消失问题求助
Hey there, let’s work through this Tab Bar disappearance issue you’re hitting!
The Issue Breakdown
You’ve got an iOS app with a Tab Bar, where one tab hosts a custom camera view. After capturing an image, you segue to a preview View Controller to display the shot. But when you tap your custom back button (using a segue named segCancelImage) to return to the camera tab, the Tab Bar vanishes entirely.
Why This Happens
The root problem is that using a new segue to navigate back creates a fresh instance of your camera View Controller instead of returning to the original one that was embedded in the Tab Bar Controller. This new, standalone instance doesn’t have the Tab Bar associated with it—hence why it’s missing.
Solutions to Fix This
1. Use dismiss(animated:completion:) (Simplest Fix)
If your preview View Controller is presented modally (which is the standard approach for this flow), the correct way to go back is to dismiss it, not trigger another segue. Replace your cancel button code with this:
@IBAction func cancelCapture(_ sender: Any) { // Dismiss the preview VC to return to the original camera tab instance dismiss(animated: true, completion: nil) }
This will unwind you back to the existing camera View Controller that’s part of the Tab Bar stack, so the Tab Bar stays visible.
2. Use an Unwind Segue (If You Need to Pass Data Back)
If you need to send data from the preview VC back to the camera VC (e.g., saving the captured image), an unwind segue is the way to go:
- Step 1: Add this unwind action to your camera View Controller:
@IBAction func unwindToCamera(segue: UIStoryboardSegue) { // Optional: Handle data passed back from the preview VC here // Example: if let previewVC = segue.source as? PreviewImageViewController { ... } } - Step 2: In Storyboard, control-drag from your cancel button to the Exit icon (the green square at the top of the preview View Controller), then select
unwindToCamera(segue:)from the dropdown. - Step 3: Delete the old
segCancelImagesegue—you won’t need it anymore.
3. Verify Tab Bar Visibility Settings
Just to cover all bases, make sure your camera View Controller isn’t set to hide the Tab Bar:
- In code (add this to your camera VC’s
viewDidLoad):override func viewDidLoad() { super.viewDidLoad() hidesBottomBarWhenPushed = false } - Or in Storyboard: Select your camera VC, go to the Attributes Inspector, and uncheck the "Hides Bottom Bar on Push" option.
Final Notes
Avoid using segues to navigate backward—they’re meant for moving forward in your app’s flow. Stick to dismiss for modally presented screens or unwind segues when data needs to be passed back. These methods will keep your Tab Bar Controller’s hierarchy intact, so the Tab Bar stays visible as expected.
Give these steps a try, and your Tab Bar should be back where it belongs when you return to the camera view!
内容的提问来源于stack exchange,提问作者eddy123

