2

This question is no duplicate to:


I'd like to hide the system volume HUD while adjusting the volume programmately without the need of a loaded View.

img


So this solution is not what I'd like to get (in my case this is not acceptable):

let volumeView = MPVolumeView(frame: .zero)
view.addSubview(volumeView)

I'd like to get a working function to hide the volumeView while avoiding to add hidden Subview instead.

This is what I've got so far (however this is not working currently) :

func hideHUD() {
  let volumeWindow: UIWindow = UIWindow()
  let volumeView = MPVolumeView(frame: .zero)

  volumeView.isHidden = false
  volumeWindow.isHidden = true
  volumeWindow.addSubview(volumeView) 
}

Note: I'm pretty sure It is possible to hide the volume HUD programmately because the VolumeBar API (you can find here) is able to hide the HUD and replace it with it's own style!

  • As I've gathered from reading around SO, the method you're trying to use only work when an AVAudioSession is active (video probably works too). So that's a necessity to make it work this way. – JillevdW Mar 09 '18 at 16:17
  • Added this line - but will also affect nothing of my code: `try AVAudioSession.sharedInstance().setActive(true)` –  Mar 09 '18 at 16:18
  • Also add a category: `try! AVAudioSession.sharedInstance().setCategory(.ambient, mode: .default, options: [])` – arnoapp Jan 06 '19 at 19:11

1 Answers1

0

It seems like what you want may not be possible. The VolumeBar project you linked uses an invisible MPVolumeView to hide the system HUD just like others have suggested. See here

/// A standard iOS `MPVolumeView` that never appears but is necessary to hide the system volume HUD.
private let systemVolumeView: MPVolumeView

...

// Add a non-hidden MPVolumeView with a zero frame to prevent the system volume HUD from showing
systemVolumeView = MPVolumeView(frame: .zero)
systemVolumeView.isHidden = false
systemVolumeView.clipsToBounds = true
systemVolumeView.showsRouteButton = false
systemVolumeView.alpha = 0.0001

Source: https://github.com/gizmosachin/VolumeBar/blob/master/Sources/Internal/VolumeBarWindow.swift

Julian Fortune
  • 172
  • 1
  • 7