2

I´ve looked around but can´t find a solution...

I have a MPVolumeView in my app to control the unit´s system volume. I changed the MPVolumeView SliderThumbImage to an image of my own and what I´ve noticed is 2 bugs:

Bug 1 : The thumb image sometimes gets offset horizontally to the right. this usually happens when I run from Xcode and the phone´s volume is at maximum. see the image where the bug happened and I bring the volume down to a minimum. if I bring the volume to minimum, close the app and reopen it, it will be put at the correct location. I think it may have something to do with how the units volume translates into a value for the slider and where the image gets positioned according to said value, but I´m unsure how to solve this. Bug 1

Bug 2: Apple´s own volume indicator layer sometimes shows up and sometimes does not. I would rather it didn´t at all.

I use a view in the storyboard which I classed MPvolumeView and then in the viewdidload I use the following code to put the image

    let faderknob : UIImage = UIImage.init(imageLiteralResourceName:"faderknob50.png")

    func setVolumeThumbImage(_ image: UIImage?,for state: UIControl.State){
        volumeView.setVolumeThumbImage(faderknob, for: .normal)
        volumeView.showsRouteButton = false
    }

    setVolumeThumbImage(faderknob, for: .normal)

any help on how to fix theses 2 bugs would be great! Thanks

Sayooj
  • 375
  • 3
  • 13

1 Answers1

2

Was also experiencing your first bug: how higher the volume, how greater the offset of the thumbimage to the right on intial appearance of the volumeview. When the app is relaunched from the background the offset is gone.

When overriding volumeSliderRect in a subclass, the problem is solved:

import MediaPlayer

class VolumeView: MPVolumeView {
    override func volumeSliderRect(forBounds bounds: CGRect) -> CGRect {
        return bounds
    }
}

A side effect is that the thumb image now gets animated from the left to the initial position on first appearance. Actually looks not too bad in my opinion.

As you now have your own subclass, you can set your custom thumbimage during init:

import MediaPlayer

class VolumeView: MPVolumeView {

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        commonInit()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }

    func commonInit() {
        setValue(false, forKey: "showsRouteButton")  // no deprecated warning
        setVolumeThumbImage(UIImage(named: "volume.slider.thumb"), for: .normal)
    
    }

    override func volumeSliderRect(forBounds bounds: CGRect) -> CGRect {
        return bounds
    }

}
kjellie
  • 352
  • 3
  • 7