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

后端 未结 2 1114
渐次进展
渐次进展 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:45

    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:

提交回复
热议问题