How do you implement an MPVolumeView?

丶灬走出姿态 提交于 2019-11-30 03:01:45

问题


I want the user to be able to change the system volume with a slider, and I realized the only way to do this is with an MPVolumeView.

But I can't find any example code for it, and every method I try to implement won't show up.

So what is the easiest and correct, working way of implementing a MPVolumeView?


回答1:


Place it as a regular slider, then use the inspector to set the class to MPVolumeView. It'll still be shown as a regular slider in IB, but at runtime, it will be an instance of MPVolumeView and will have the necessary styles and behavior.




回答2:


Use this it will automatically get it

mpVolumeViewParentView.backgroundColor = [UIColor clearColor];
MPVolumeView *myVolumeView = [[MPVolumeView alloc] initWithFrame: mpVolumeViewParentView.bounds];
[mpVolumeViewParentView addSubview: myVolumeView];
[myVolumeView release];



回答3:


In iOS 13 this has changed. Adding a slider in IB with its class set to MPVolumeView doesn't work anymore. So the accepted answer no longer works. The right way, as outlined in the Apple docs, is to use a UIView in IB and then in code add the MPVolumeView as a subview. Here's how in Swift:

// myVolumeViewParentView is the UIView you put in IB
let myVolumeView = MPVolumeView(frame: myVolumeViewParentView.bounds)
myVolumeViewParentView.addSubview(myVolumeView)

This method works in iOS 12 too.



来源:https://stackoverflow.com/questions/2795567/how-do-you-implement-an-mpvolumeview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!