xcode UIWebView 中横向模式的 Youtube 视频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8111207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:24:34  来源:igfitidea点击:

Youtube video in landscape mode within a UIWebView

iphoneobjective-ciosxcodeuiwebview

提问by anasaitali

My application was not made ??for the landscape. But when I open my YouTube channel in a UIWebViewand 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.

我的应用程序不是为景观而制作的。但是当我在UIWebView 中打开我的 YouTube 频道并且用户启动视频时,它出现在纵向。如果用户旋转他的 iPhone,我想让它以横向模式出现。

How to enable landscapemode 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.

我知道有“肮脏的黑客”可以做到这一点,但我更喜欢更干净的东西。另外我不希望 UIWebView 切换到横向,而只是视频可以。

采纳答案by anasaitali

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));
}

回答by dj912

-(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

//这适用于ios8

回答by Ajeet Pratap Maurya

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

要在横向视图中显示您的视频,只需转到您在 Xcode 中实现 webView 的视图的属性检查器,并将方向从纵向更改为横向。就这么简单..享受

and dont forget to add this code

并且不要忘记添加此代码

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {   
            return YES;  
    } 
}