Ios 如何显示AirPlay菜单快捷界面
感谢airplay音频系统名emoji,我制作了一个不错的图标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中使用它
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()
}