How to customize MPVolumeView?

前端 未结 5 1602
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-30 23:48

I have tried many methods to implement a regular UISlider and control the device volume, but it\'s all Native-C functions which results in many untraceable bugs.

相关标签:
5条回答
  • 2021-01-31 00:14

    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:

    0 讨论(0)
  • 2021-01-31 00:15

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

    0 讨论(0)
  • 2021-01-31 00:22

    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

    0 讨论(0)
  • 2021-01-31 00:25

    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.

    0 讨论(0)
  • 2021-01-31 00:26

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


    [EDIT]

    Try this.

    0 讨论(0)
提交回复
热议问题