I have had a hard time with this problem as well. I came across this project https://github.com/jpsim/JPSVolumeButtonHandler and it does the job for the most part and even hides the volume indicator when the volume buttons are pressed.
This is what I did to get it to work. I hope that it helps.
import SwiftUI
import JPSVolumeButtonHandler
struct MyView: View {
@State private var volumeHandler: JPSVolumeButtonHandler?
var body: some View {
ZStack {
Text("Hello World")
}
.onAppear {
// Setup capture of volume buttons
volumeHandler = JPSVolumeButtonHandler(up: {
// Code to run when volume button released
}, downBlock: {
// Code to run when volume button pressed
})
// Start capture of volume buttons
volumeHandler?.start(true)
}
.onDisappear {
// Stop capture of volume buttons when leaving view
volumeHandler?.start(false)
}
}
}
This is the post I used to figure it out originally: iOS. Capturing photos with volume buttons