Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>After accepting @Erik B's answer and awarding the bounty to him, I found that there was more tweaking necessary to get it to work. I am posting here for the benefit of future SO searchers.</p> <p>The problem I was seeing was that the internal mechanisms of the buttons would assign the image based on the current airplay state. Thus any customizations I made during init would not stick if the Airplay receiver went away, or the state was changed somehow. To solve this, I setup a KVO observation on the button's <code>alpha</code> key. I noticed that the button is always faded in/out which is an animation on <code>alpha</code>.</p> <pre><code>MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:CGRectZero]; [volumeView setShowsVolumeSlider:NO]; for (UIButton *button in volumeView.subviews) { if ([button isKindOfClass:[UIButton class]]) { self.airplayButton = button; // @property retain [self.airplayButton setImage:[UIImage imageNamed:@"airplay.png"] forState:UIControlStateNormal]; [self.airplayButton setBounds:CGRectMake(0, 0, kDefaultIconSize, kDefaultIconSize)]; [self.airplayButton addObserver:self forKeyPath:@"alpha" options:NSKeyValueObservingOptionNew context:nil]; } } [volumeView sizeToFit]; </code></pre> <p>Then I observe the changed value of the buttons <code>alpha</code>. </p> <pre><code>- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if ([object isKindOfClass:[UIButton class]] &amp;&amp; [[change valueForKey:NSKeyValueChangeNewKey] intValue] == 1) { [(UIButton *)object setImage:[UIImage imageNamed:@"airplay.png"] forState:UIControlStateNormal]; [(UIButton *)object setBounds:CGRectMake(0, 0, kDefaultIconSize, kDefaultIconSize)]; } } </code></pre> <p>Don't forget to remove the observer if you destroy the button</p> <pre><code>- (void)dealloc { [self.airplayButton removeObserver:self forKeyPath:@"alpha"]; … } </code></pre> <p>Based on code observation, the button will break if Apple changes the internal view hierarchy of the <code>MPVolumeView</code> to add/remove/alter the views such that a different button comes up. This makes it kind of fragile, so use at your own risk, or come up with a plan b in case this happens. I have been using it for over a year in production with no issues. If you want to see it in action, check out the main player screen in <a href="http://ambianceapp.com/iphone" rel="noreferrer">Ambiance</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload