Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何显示AirPlay菜单快捷界面_Ios_Swift_Swiftui_Ios13_Airplay - Fatal编程技术网

Ios 如何显示AirPlay菜单快捷界面

Ios 如何显示AirPlay菜单快捷界面,ios,swift,swiftui,ios13,airplay,Ios,Swift,Swiftui,Ios13,Airplay,感谢airplay音频系统名emoji,我制作了一个不错的图标 Button(action: { showAirplay() }, label: { Image(systemName: "airplayaudio") .imageScale(.large) }) func showAirplay() { ??? } 但我不知道如何展示著名的菜单: 最后我自己解决了:D 正如评论中所说,我必须“将其嵌入”UIKit并在SwiftUI中使用它

感谢airplay音频系统名emoji,我制作了一个不错的图标

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

func showAirplay() {
       ???
}
但我不知道如何展示著名的菜单:


最后我自己解决了:D 正如评论中所说,我必须“将其嵌入”UIKit并在SwiftUI中使用它

首先:

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

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

    }
}

最后,在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)

        }
    }
}

包装AirPlay UIView for SwiftUI似乎是最好、最简单的解决方案

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) {
    }
}
用法:

VStack {
  AirPlayView()
}

试图使UIViewControllerRepresentable嵌入:但在我这方面似乎不起作用…谢谢。这个解决方案对我有效。
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) {
    }
}
VStack {
  AirPlayView()
}