将MPVolumeView添加到底部工具栏

时间:2012-12-01 21:42:27

标签: ios subview mpvolumeview

我正在尝试将MPVolumeView的实例添加到导航控制器的底栏。(勾选工具栏的框) 我的代码没有任何错误,但是当我在设备上运行项目时,音量滑块没有显示。提前感谢任何建议,这是我的代码:

@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 个答案:

答案 0 :(得分:2)

您无法将子视图添加到工具栏。您必须添加一个UIBarButtonItem,其视图是您要显示的视图。

UIBarButtonItem* b = [[UIBarButtonItem alloc] initWithCustomView: myVolumeView];
myToolbar.items = @[b];

这与MPVolumeView无关。您想要添加到工具栏或导航栏的任何任意视图都是如此。

这是我的设备上用于导航界面中的视图控制器的实际代码:

MPVolumeView* vv = [[MPVolumeView alloc] initWithFrame: CGRectMake(0, 0, 150, 40)];
UIBarButtonItem* b = [[UIBarButtonItem alloc] initWithCustomView: vv];
self.toolbarItems = @[b];
self.navigationController.toolbarHidden = NO;

另请注意,您必须在设备上进行测试;模拟器中的卷视图没有界面。 :(

相关问题