How to customize MPVolumeView?

这一生的挚爱 提交于 2019-12-02 16:21:10

You could try cycling through its subviews and look for a UISlider subclass?

Since iOS 5.0 you can use UIAppearance on a UISlider, even when part of MPVolumeView.

Anywhere in your codebase:

[[UISlider appearanceWhenContainedIn:[MPVolumeView class], nil] setMinimumTrackImage:[[UIImage imageNamed:@"nowplaying_bar_full.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 25, 5, 25)] forState:UIControlStateNormal];
[[UISlider appearanceWhenContainedIn:[MPVolumeView class], nil] setMaximumTrackImage:[[UIImage imageNamed:@"nowplaying_bar_empty.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 25, 5, 25)] forState:UIControlStateNormal];
[[UISlider appearanceWhenContainedIn:[MPVolumeView class], nil] setThumbImage:[UIImage imageNamed:@"nowplaying_player_nob.png"] forState:UIControlStateNormal];

Here a list of some of the other classes that can be implemented using UIAppearance: https://gist.github.com/mattt/5135521

There are now ways to accomplish this, simply use:

– setMaximumVolumeSliderImage:forState:
– setMinimumVolumeSliderImage:forState:
– setVolumeThumbImage:forState:

Which are slightly different method names than the ones for the vanilla UISlider.

This prevents you from having to cycle through the views and potentially have something break in the future as Apple changes things.

Answer 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:

Stephen Furlani

Try using a Notification, but it looks like Apple is denying them.


[EDIT]

Try this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!