12
votes

My application was not made ​​for the landscape. But when I open my YouTube channel in a UIWebView and a user launches a video, it appears in Portrait. I would like to make it appear in landscape mode if the user rotate his iPhone.

How to enable landscape mode as in this case?

I know there are "dirty hacks" to do this but I prefer something cleaner. Also I do not want the UIWebView switches to landscape but just that the videos can.

3
ps. 'Normal' mode is 'Portrait' – Dan Hanly
@DanielHanly Yes ^^ English is not my native language. – anasaitali

3 Answers

2
votes

I finally adjusted my view so that it supports landscape mode using the following code :

- (void)viewDidLoad {
    [super viewDidLoad];

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:)
                                                 name:UIDeviceOrientationDidChangeNotification object:nil];
}

- (void)viewWillAppear:(BOOL)animated {
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(deviceOrientation))
    {
        //I set my new frame origin and size here for this orientation
        isShowingLandscapeView = YES;
    }
    else if (deviceOrientation == UIDeviceOrientationPortrait)
    {
        //I set my new frame origin and size here for this orientation
        isShowingLandscapeView = NO;
    }
}

- (void)orientationChanged:(NSNotification *)notification
{
    // We must add a delay here, otherwise we'll swap in the new view
    // too quickly and we'll get an animation glitch
    [self performSelector:@selector(updateLandscapeView) withObject:nil afterDelay:0];
}

- (void)updateLandscapeView
{
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(deviceOrientation) && !isShowingLandscapeView)
    {
        //I set my new frame origin and size here for this orientation
        isShowingLandscapeView = YES;
    }
    else if (deviceOrientation == UIDeviceOrientationPortrait && isShowingLandscapeView)
    {
        //I set my new frame origin and size here for this orientation
        isShowingLandscapeView = NO;
    }    
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait || UIInterfaceOrientationIsLandscape(interfaceOrientation));
}
0
votes

To show your video in landscape view, just go to the attribute inspector of that view where you have implemented your webView in Xcode and change the orientation from portrait to landscape. as simple as that..enjoy

and dont forget to add this code

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {   
            return YES;  
    } 
} 
0
votes
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {

id presentedViewController = [window.rootViewController presentedViewController];
NSString *className = presentedViewController ? NSStringFromClass([presentedViewController class]) : nil;
if (window && [className isEqualToString:@"AVFullScreenViewController"] && [presentedViewController isBeingDismissed] == NO) {
    return UIInterfaceOrientationMaskAll;
} else {
    return UIInterfaceOrientationMaskPortrait;
}}

//This Works for ios8