Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set the tint color of a MPVolumeView?

How can I set the tint color of a MPVolumeView?

I see the method:

setMinimumTrackTintColor: 

and the property

minimumTrackTintColor

however using either of those throws an exception.

I was told I need to call

setMinimumTintColor 

or set the property

minimumTrackTintColor 

on the underlying UISlider, however I don't know how to do that.

Thanks!

like image 289
Phillip Avatar asked May 10 '14 05:05

Phillip


2 Answers

In iOS 7 you simply can change the slider color of a MPVolumeView like this:

CGRect rect1 = CGRectMake(17, 45, 220, 0);
MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:rect1];

volumeView.tintColor = [UIColor colorWithRed:0.0f/255.0f green:255.0f/255.0f blue:127.0f/255.0f alpha:1.0f];

Hope this helps.

like image 96
kchromik Avatar answered Oct 11 '22 17:10

kchromik


I have searched for answer to this for a very long time, because in my app I need to set volumeView colors dynamically. Since UISlider provides a way to change its tint color, you can cycle through MPVolumeView subviews until you find an instance of UISlider class and then operate on this one.

Here is the code in Swift:

func customSlider() {
        let temp = mpVolView.subviews
        for current in temp {
            if current.isKind(of: UISlider.self) {
                let tempSlider = current as! UISlider
                tempSlider.minimumTrackTintColor = .yellow
                tempSlider.maximumTrackTintColor = .blue
            }
        }
    }

Result:

enter image description here

like image 2
Adam Avatar answered Oct 11 '22 16:10

Adam