您可以通过外观方法设置 MPVolumeView 的最小和最大值图像。以下是如何在Objective-C中执行此操作:
如果您只支持 iOS 9.0+:
[[UISlider appearanceWhenContainedInInstancesOfClasses:@[[MPVolumeView class]]] setMinimumValueImage:[UIImage imageNamed:@"VolumeDown"]];
[[UISlider appearanceWhenContainedInInstancesOfClasses:@[[MPVolumeView class]]] setMaximumValueImage:[UIImage imageNamed:@"VolumeUp"]];
或者对于 5.0 之后的任何 iOS 版本:
if( [UISlider respondsToSelector:@selector(appearanceWhenContainedInInstancesOfClasses:)] )
{
[[UISlider appearanceWhenContainedInInstancesOfClasses:@[[MPVolumeView class]]] setMinimumValueImage:[UIImage imageNamed:@"VolumeDown"]];
[[UISlider appearanceWhenContainedInInstancesOfClasses:@[[MPVolumeView class]]] setMaximumValueImage:[UIImage imageNamed:@"VolumeUp"]];
}
else
{
[[UISlider appearanceWhenContainedIn:[MPVolumeView class], nil] setMinimumValueImage:[UIImage imageNamed:@"VolumeDown"]];
[[UISlider appearanceWhenContainedIn:[MPVolumeView class], nil] setMaximumValueImage:[UIImage imageNamed:@"VolumeUp"]];
}
在斯威夫特:
UISlider.appearance(whenContainedInInstancesOf: [MPVolumeView.self]).minimumValueImage = UIImage(systemName: "speaker.fill")
UISlider.appearance(whenContainedInInstancesOf: [MPVolumeView.self]).maximumValueImage = UIImage(systemName: "speaker.wave.3.fill")