iPhone rotation and full-screen video

2019-05-20 08:31发布

问题:

I've encountered a strange issue, which I need help resolving. My app is always run in portrait mode - I explicitly want it to be that way. In one place in the app I have a UIWebView, which works just fine, as expected. This web view is not shown all the time but is dynamically added to the main view and removed based on user actions (i.e. it's only visible when I need it to).

Now, sometimes in this view I may have a youtube video. The WebView simply contains the <iframe> for the youtube embed. When a user clicks on the video preview frame in the webview, a full-screen video viewer is launched to play the video - which is just fine.

However if during the playback the user rotates the phone, the full-screen video player is rotated and the video is played in landscape mode. Now the video is stopped and the user presses "Done" button without rotating the phone back to portrait mode, the video player is closed and the user returns to my app - however now my layout is also rotated! Not just the video view, but the entire layout - with toolbar, navigation controller, etc.

I don't want any rotation! I just want everything in my app to remain in portrait mode! Moreover, now even if the phone is rotated back to portrait, the app stays in landscape mode.

Note that I only tested this in a simulator so far, as I currently don't have a physical phone to test. I created a simple (bad quality!) video (just filmed the simulator on my screen with an old phone camera) to demonstrate the issue - the video is here: http://shchuka.com/hosting/rotation_problem.mp4

Any ideas what I can do about it?

回答1:

Add this in, or change it:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    return (UIInterfaceOrientation == UIInterfaceOrientationPortrait);

}

That will prevent all rotation and the app will only work in portrait mode. You can place the return statement within IF statements to allow rotation under certain circumstances.



回答2:

I sometimes add views to the appDelegate window when I want them to be above everything else, and an annoying side-effect has been that I have to explicitly call my own rotation code as there is no VC as such to deal with it. On the plus side, it might mean that it could be ideal for your needs -

[[[[UIApplication sharedApplication] delegate] window] addSubview:myWebView];

...depending on your app's orientation, etc, before you load the view in the first place, you might need to rotate the webView before displaying it.