自定义一个全局的button,然后添加MPVolumeView拿到里面的button进行更换。
MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(260, 407, 40, 40)];
[volumeView setShowsVolumeSlider:NO];
for (UIButton *button in volumeView.subviews) {
if ([button isKindOfClass:[UIButton class]]) {
self.airplayButton = button;
self.airplayButton.showsTouchWhenHighlighted = NO;
[self.airplayButton setImage:[UIImage imageNamed:@"fav_audios_airplay.png"]forState:UIControlStateNormal];
[self.airplayButton setBounds:CGRectMake(0, 0, 40, 40)];
[self.airplayButton addObserver:self forKeyPath:@"alpha" options:NSKeyValueObservingOptionNewcontext:nil];
}
}
[volumeView sizeToFit];
[self.view addSubview:volumeView];
还需要添加这一段来设置多一次图片和大小
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([object isKindOfClass:[UIButton class]] && [[change valueForKey:NSKeyValueChangeNewKey] intValue] ==1) {
[(UIButton *) object setImage:[UIImage imageNamed:@"fav_audios_airplay.png"]forState:UIControlStateNormal];
[(UIButton *) object setBounds:CGRectMake(0, 0, 40, 40)];
}
}
在dealloc里面移除
- (void)dealloc {
[self.airplayButton removeObserver:self forKeyPath:@"alpha"];
}