[BUG] Fields in View hide the camera controls
I have this view
struct MyCamera: View {
@Environment(\.dismiss) private var dismiss
var body: some View {
MCamera()
.setAudioAvailability(false)
.startSession()
}
}
it produces this screen
However I was expecting this screen:
I noticed that the controls become invisible (like in the first picture) different kinds of fields in the view. So when I remove @Environment(\.dismiss) private var dismiss it works fine.
It also does not work with this line @State private var viewModel = ViewModel() with ViewModel being any type of class. If I leave out @State it does not work either.
However I could observe, that a simple variable e.g. holding a Bool was not showing the same result.
I use iOS 18.2, Xcode 16.2 and Camera Version 3.0.1
Just wanted to chime in and mention I'm experiencing this as well.
This is probably not tied with @Environment(\.dismiss) private var dismiss but with
onDisappear() on MCamera. A good way to reproduce this is to open the camera, go to other app (or minimize your app) then open the app again. The session is active but the buttons are gone
Just want to say I have the same issue. I tried to use controller.reopenCameraScreen() inside onImageCaptured but the controls also disappears after the first image.
This is probably not tied with
@Environment(\.dismiss) private var dismissbut withonDisappear()onMCamera. A good way to reproduce this is to open the camera, go to other app (or minimize your app) then open the app again. The session is active but the buttons are gone
I just reproduced it too, it's really a serious bug but don't know why I did not notice it :) might be because I have never switched the app during opening camera. I just experienced a same result but it happens in a other case (see #88).
Hey! Any news here on potential solution? This seems like a top priority bug
Hey! Any news here on potential solution? This seems like a top priority bug
It looks like the author has abandonned this project, I have fixed a bug myself and did another workaround (since I haven't have time to investigate it deeper).
@tiendq Can you share/contribute the potential solution ?
My investigation showed that the problem is the AVCaptureSession being stopped when the app is suspended.
By some reason top and bottom controls are only shown when the sessions is running.
So in order to restart the session after the app moves to foreground, you may do this:
extension CameraManagerNotificationCenter {
func setup(parent: CameraManager) {
self.parent = parent
NotificationCenter.default.addObserver(self, selector: #selector(handleSessionWasInterrupted), name: .AVCaptureSessionWasInterrupted, object: parent.captureSession)
NotificationCenter.default.addObserver(self, selector: #selector(resumeSession), name: UIApplication.willEnterForegroundNotification, object: nil)
}
@objc private func resumeSession() {
let session = parent.captureSession
DispatchQueue.global(qos: .userInitiated).async {
if !session.isRunning {
session.startRunning() // Restart session when app enters foreground
}
}
}
}
Also, the AVCaptureSession may be stopped by other reasons like System Resource Pressure or hardware failure.
Those are rare, but I would always show at least the top bar to make sure the user can press "X" and continue using the app without restarting:
--- a/Sources/Internal/UI/Default Screens/Camera/DefaultCameraScreen+TopBar.swift
+++ b/Sources/Internal/UI/Default Screens/Camera/DefaultCameraScreen+TopBar.swift
@@ -15,7 +15,7 @@ extension DefaultCameraScreen { struct TopBar: View {
let parent: DefaultCameraScreen
- var body: some View { if isTopBarActive {
+ var body: some View {
ZStack {
createCloseButton()
createCentralView()
@@ -27,7 +27,7 @@ extension DefaultCameraScreen { struct TopBar: View {
.padding(.horizontal, 20)
.background(Color(.mijickBackgroundPrimary80))
.transition(.move(edge: .top))
- }}
+ }
}}
@bogdan Thanks for detailed explanation
Also encountering the same issue of camera controls disappearing when bringing the app back to the foreground. Additionally, if I present the camera too early when cold launching the app I get the same behavior, making it a bit difficult to quickly navigate to the camera experience in my app upon launch.
I encountered the same issue where camera controls disappear after the app goes to background and returns to foreground.
Solution
Force view recreation by monitoring scenePhase transitions and regenerating a UUID key:
struct CameraView: View {
@Environment(\.scenePhase) private var scenePhase
@State private var cameraKey = UUID()
var body: some View {
MCamera()
// ... your existing configuration
.id(cameraKey)
.onChange(of: scenePhase) { oldPhase, newPhase in
handleScenePhaseChange(from: oldPhase, to: newPhase)
}
}
private func handleScenePhaseChange(from oldPhase: ScenePhase, to newPhase: ScenePhase) {
switch (oldPhase, newPhase) {
case (.inactive, .active):
Task {
// Short delay to let app settle
try? await Task.sleep(nanoseconds: 350_000_000)
await MainActor.run {
cameraKey = UUID() // Forces view recreation
}
}
default:
break
}
}
}
How it works
- Detects when app returns from background (
.inactive→.active) - Waits 0.35s for the app to settle
- Generates new UUID to force SwiftUI to recreate the entire MCamera view
- This reinitializes the camera session and restores all UI controls
Hope this helps others experiencing the same issue! 👍