I am writing an app for tvOS that launches videos from URL's into an AVPlayerView, which is not nested in another View. The trouble is, the user had no idea anything was going on while loading was occurring, so I wanted to add an activity indicator in the center of the screen.
Getting the screen center is not straightforward while a video is loading, however, because the view's bounds are set to (0, 0, 0, 0) while the video is loading, so setting the subview's center = view.center doesn't work (it will just add my subviews in the upper left corner of the screen).
How can I add a subview into the center of the screen, when the frame is set to (0, 0, 0, 0)?
Luckily, I was able to find the answer on my own. UIScreen works just as well with tvOS as it would on an iOS device. My complete solution is below, hope it helps save someone some time. I am including my observers that trigger the end as well.
The class that I used to handle the activity
This is the part where you use UIScreen to get the bounds
I added a label to add some clarity
And then in the AVPlayerViewController that you want to add some the loading activity to...
First create the activity indicator object:
Create your asset, and playerItem (I set some player item keys)
start the activity indicator animations
Add notification to player and start playback
Finally, handle the observer:
Hope this helps someone else who runs into some of the same issues I did.