iOS - How can I display an 'AirPlay' popup menu in Swift?

后端 未结 2 1132
渐次进展
渐次进展 2021-01-05 17:54

How can I display an AirPlay popup menu in my Swift project? (Many applications like Spotify can display one like below):

2条回答
  •  轮回少年
    2021-01-05 18:42

    Here's a sweet little workaround to having to use the MPVolumeView's button.

    1. Create a MPVolumeView, and hide it somewhere in the view hierarchy.
    2. Whenever you want to display the picker:

    [[UIApplication sharedApplication] sendAction:NSSelectorFromString(@"_displayAudioRoutePicker") to:myVolumeView from:myView forEvent:nil];

    Optional 3: On iPad you'll need to pass a UIEvent otherwise the popover will just be entered at the top of the screen and it'll look wonky. Capture the event from - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; and pass it into our call.

提交回复
热议问题