Using AVRoutePickerView, I'm able to do the Airplay programmatically upon tapping the Airplay icon. Now I want, on viewDidLoad it should initialize the AVRoutePickerView and even tap the airplay icon (no need to manually tap on icon).
Here's my small piece of code to display Airplay icon on UIView
let routePV = AVRoutePickerView(frame: CGRect(x: 30.0, y: 150.0, width: 30.0, height: 30.0)) routePV = UIColor.clear
self.view.addSubview(routePV)
I'm really not sure how to trigger tap on viewdidload ?
Thanks
This is possible. However I must caution it is likely to be quite unexpected behavior to present this interface without user interaction. I’d recommend only doing this as a direct result of the user intending to present this interface.
Make sure you have added it to your view hierarchy.
let routePickerView = AVRoutePickerView()
view.addSubview(routePickerView)
If desired, you can hide it.
routePickerView.isHidden = true
Then to present the route picker interface, trigger touchUpInside
on its UIButton
:
if let routePickerButton = routePickerView.subviews.first(where: { $0 is UIButton }) as? UIButton {
routePickerButton.sendActions(for: .touchUpInside)
}
Do note that this could stop working in a future iOS release.
I prefer to use an extension:
fileprivate extension AVRoutePickerView {
func present() {
let routePickerButton = subviews.first(where: { $0 is UIButton }) as? UIButton
routePickerButton?.sendActions(for: .touchUpInside)
}
}
Example implementation:
class RandomViewController: UIViewController {
...
private lazy var routePickerView = {
let routePickerView = AVRoutePickerView(frame: .zero)
routePickerView.isHidden = true
view.addSubview(routePickerView)
return routePickerView
}
...
@IBAction func presentPickerView(_ sender: UIButton) {
routePickerView.present()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With