如何显示 AirPlay 菜单 SwiftUI

Lor*_*Foe 0 ios airplay swift ios13 swiftui

感谢 airplay 音频系统名称表情符号我做了一个漂亮的图标

Button(action: {
        showAirplay()
}, label: {
    Image(systemName: "airplayaudio")
        .imageScale(.large)
})

func showAirplay() {
       ???
}
Run Code Online (Sandbox Code Playgroud)

但我不知道如何显示著名的菜单:

Lor*_*Foe 6

最后我设法自己解决了这个问题:D 就像评论中所说的那样,我必须“将其嵌入到”UIKit 中并在 SwiftUI 中使用它

首先 :

struct AirPlayButton: UIViewControllerRepresentable {
    func makeUIViewController(context: UIViewControllerRepresentableContext<AirPlayButton>) -> UIViewController {
        return AirPLayViewController()
    }

    func updateUIViewController(_ uiViewController: UIViewController, context: UIViewControllerRepresentableContext<AirPlayButton>) {

    }
}

Run Code Online (Sandbox Code Playgroud)

然后是经典的 ViewController,我们很久以来就知道如何显示这个著名的 AirPlay 菜单弹出窗口:

class AirPLayViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let isDarkMode = self.traitCollection.userInterfaceStyle == .dark

        let button = UIButton()
        let boldConfig = UIImage.SymbolConfiguration(scale: .large)
        let boldSearch = UIImage(systemName: "airplayaudio", withConfiguration: boldConfig)

        button.setImage(boldSearch, for: .normal)
        button.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
        button.backgroundColor = .red
        button.tintColor = isDarkMode ? .white : .black

        button.addTarget(self, action: #selector(self.showAirPlayMenu(_:)), for: .touchUpInside)
        self.view.addSubview(button)
    }

    @objc func showAirPlayMenu(_ sender: UIButton){ // copied from /sf/answers/3143661181/
        let rect = CGRect(x: 0, 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()
    }
}

Run Code Online (Sandbox Code Playgroud)

最后在 SwiftUI 中只需调用:

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Hello World")
            AirPlayButton().frame(width: 40, height: 40) // (important to be consistent with this frame, like that it is nicely centered... see button.frame in AirPlayViewController)

        }
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

为 SwiftUI 包装 AirPlay UIView 似乎是最好和最简单的解决方案。

struct AirPlayView: UIViewRepresentable {

    func makeUIView(context: Context) -> UIView {

        let routePickerView = AVRoutePickerView()
        routePickerView.backgroundColor = UIColor.clear
        routePickerView.activeTintColor = UIColor.red
        routePickerView.tintColor = UIColor.white

        return routePickerView
    }

    func updateUIView(_ uiView: UIView, context: Context) {
    }
}
Run Code Online (Sandbox Code Playgroud)

用法 :

VStack {
  AirPlayView()
}
Run Code Online (Sandbox Code Playgroud)