brainforked
brainforked

Reputation: 139

View not visible on UIWindow when added for the second time

I am using this code

UIWindow *window = [[UIApplication sharedApplication].windows objectAtIndex:0];
UIView * videoView = [[window subviews] lastObject];
[videoView addSubview:viewFullScreenToolbar];

to add a view on topmost view to play video on fullscreen. When the fullscreen button is pressed, this code executes and presents a fullscreen video. But when fullscreen is closed and then pressed again, the viewFullScreenToolbar is not visible although the video is playing correctly in fullscreen. Plus the problem is only in landscape mode, it works good in portrait mode.

Upvotes: 0

Views: 2747

Answers (4)

brainforked
brainforked

Reputation: 139

Its seems so obvious once you find the answer. So, all I had to do was register for the orientation change notification.

NSNotificationCenter* center = [NSNotificationCenter defaultCenter];

[center addObserver:self selector:@selector(ChangeOrientationIfFullScreen:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];

And in the method called when the notification is received, I had to set the frames of the subviews manually for each orientation change. It was a bit of overhead but finally achieved a smooth transition on the screen.

Upvotes: 0

Jesse
Jesse

Reputation: 1697

Are you trying to add a subview to the fullscreen MPMoviePlayerController view?

  1. Yes. You do need to add the subview in a hacky way (similar to what you list) because MPMoviePlayerController provides no hooks to the fullscreen video view. Your issue is likely related to this subview not getting rotation callbacks.

  2. No. If you own this videoView then you shouldn't access the view using UIWindow. You should access it through its view controller. Then when you add your toolbar as a subview it will receive rotation callbacks.

If you situation is #1 I can help further but want to check first.

[Update]

If you want add an overlay view to the MPMoviePlayerController fullscreen view you have three options:

  1. Don't.
  2. Write your own MoviePlayer based on AVPlayer.
  3. Use the hack shown in the following sample: MoviePlayerFullscreenOverlay

Adding an overlay view to the MPMoviePlayerController fullscreen view is not straightforward because the fullscreen view is not accessible from the MPMoviePlayerController API. It requires accessing the view through the window, adding the subview and then manually handling the rotation callbacks. Pain in the $%^*.

Please do not use this approach for any other situation.

Upvotes: 0

Anubrata Santra
Anubrata Santra

Reputation: 666

This should serve your purpose--

AppDelegate *appD=(AppDelegate *)[[UIApplication sharedApplication] delegate];

[appD.window addSubview:viewFullScreenToolbar]; [appD.window bringSubviewToFront:viewFullScreenToolbar];

Upvotes: 2

Wil Shipley
Wil Shipley

Reputation: 9553

This is...not a good way to do what you want. Don’t rely on a particular window being the “first” window, and don’t rely on a a particular view being the “last” subview. Those things are too subject to change.

Create IBOutlets on the windows and views you want to use, hook them up in the .XIB file, and refer to them directly.

Upvotes: 5

Related Questions