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

旧城冷巷雨未停 提交于 2019-12-06 02:36:44

问题


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


回答1:


After all it seems there is no easy and straightforward way to make a custom button display the system's Airplay menu.

However, @totiG pointer me to an interesting resource and I created a script that creates the standard Volume Control outside of the visible area of the screen a simulates a click on the Airplay button:

func showAirplay() {
    let rect = CGRect(x: -100, y: 0, width: 0, height: 0)
    let airplayVolume = MPVolumeView(frame: rect)
    airplayVolume.showsVolumeSlider = false
    self.view.addSubview(airplayVolume)
    for view: UIView in airplayVolume.subviews {
        if let button = view as? UIButton {
            button.sendActions(for: .touchUpInside)
            break
        }
    }
    airplayVolume.removeFromSuperview()
}

After running this code the following popup menu appears:




回答2:


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.



来源:https://stackoverflow.com/questions/44879210/ios-how-can-i-display-an-airplay-popup-menu-in-swift

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