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!

有帮助吗?

解决方案

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.

其他提示

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

This worked for me to change the route button color too:

volumeView.tintColor = .green
if let routeButton = volumeView.subviews.compactMap({ $0 as? UIButton }).first,
    let image = routeButton.image(for: .normal) {
    routeButton.setImage(image.withRenderingMode(.alwaysTemplate), for: [])
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top