问题
I am trying to add an instance of MPVolumeView to the bottom bar of my navigation controller.(the box for toolbar is ticked) I do not get any errors with my code, but when I run the project on my device the volume slider is not showing. Thanks in advance for any suggestions, here is my code:
@interface ViewController ()
{
AVPlayer *vPlayer;
AVPlayerItem *playerItem;
UISegmentedControl *segm;
UIToolbar *toolbar;
}
@end
@implementation ViewController
@synthesize myViewVolume;
@synthesize nowPlaying;
- (void)viewDidLoad
{
[super viewDidLoad];
playerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:@"http://vibesradio.org:8000 /listen.pls"]];
vPlayer = [[AVPlayer alloc] initWithPlayerItem:playerItem];
[playerItem addObserver:self forKeyPath:@"timedMetadata" options:NSKeyValueObservingOptionNew context:NULL];
//self.myViewVolume = [[MPVolumeView alloc] initWithFrame:CGRectMake(20, 330, 280, 50)];
self.myViewVolume = [[MPVolumeView alloc] initWithFrame: toolbar.bounds];
[self.myViewVolume sizeToFit];
[self.view addSubview:toolbar];
[toolbar addSubview:self.myViewVolume];
}
回答1:
You cannot add subviews to a toolbar. You must add a UIBarButtonItem whose view is the view you want to show.
UIBarButtonItem* b = [[UIBarButtonItem alloc] initWithCustomView: myVolumeView];
myToolbar.items = @[b];
This has nothing to do with MPVolumeView. It's true of any arbitrary view you'd like to add to a toolbar or nav bar.
Here's actual code that works on my device for a view controller in a navigation interface:
MPVolumeView* vv = [[MPVolumeView alloc] initWithFrame: CGRectMake(0, 0, 150, 40)];
UIBarButtonItem* b = [[UIBarButtonItem alloc] initWithCustomView: vv];
self.toolbarItems = @[b];
self.navigationController.toolbarHidden = NO;
Note too that you must test on a device; there's no interface for the volume view in the Simulator. :(
来源:https://stackoverflow.com/questions/13664033/add-mpvolumeview-to-bottom-toolbar