How to customize MPVolumeView?

前端 未结 5 1601
爱一瞬间的悲伤
爱一瞬间的悲伤 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: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

提交回复
热议问题