How to add the MPVolumeView via Xcode Designer?

Devology Ltd picture Devology Ltd · May 16, 2011 · Viewed 12.3k times · Source

I originally added an MPVolumeView dynamically on to a page...

#import "MediaPlayer/MPVolumeView.h"
.
.
-(IBAction) handleVolumeButtonClicked:(id) sender {
    if (volumeView == nil) {
        volumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(25, 378, 270, 30)];
        [self.view addSubview:volumeView];
        [volumeView release];
    } else {
        [volumeView removeFromSuperview];
        volumeView = nil;
    }
}

But for some reason I started getting reports of the application crashing when dynamically adding the volume component.

To work around this, I decided to try and add the component to the view via the XCode designer, but then I realised that I didn't know how to do this!

I dragged an 'Object' from the template to the Object palette first, but then I found that I couldn't add it to the view. So I scrapped that idea and then dragged the 'View' object directly on to the .xib view.

Once the component was added, I tried to change the 'Custom Class' to 'MPVolumeView', but the view just rendered an empty rectangle. Running the code in the simulator failed to render anything.

Does anyone know what steps I am missing to add a class to the view that doesnt already exist in the palette?

Answer

Shane Arney picture Shane Arney · Aug 30, 2012

Adding a generic UIView and setting the custom class to MPVolumeView does indeed work. Make sure to do the following:

  • Add the MediaPlayer framework to your project. The loading of MPVolumeView will fail silently (albeit with a message logged to the console) if you don't.
  • Run on an actual device. The simulator won't show the volume slider.
  • Double-check that your view isn't getting autoresized out of the view.