【问题标题】:Youtube video in landscape mode within a UIWebViewUIWebView 中横向模式的 Youtube 视频
【发布时间】:2011-12-28 00:19:24
【问题描述】:

我的申请不是为了风景。但是,当我在 UIWebView 中打开我的 YouTube 频道并且用户启动视频 时,它会以纵向显示。如果用户旋转他的 iPhone,我想让它以横向模式显示。

在这种情况下如何启用横向模式?

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

【问题讨论】:

  • ps。 “正常”模式是“人像”
  • @DanielHanly 是的 ^^ 英语不是我的母语。

标签: iphone objective-c ios xcode uiwebview


【解决方案1】:

我终于使用以下代码调整了我的视图,使其支持横向模式:

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

【讨论】:

  • 干得好!但是你需要在横向调整整个 UI 吗?如果我只想在横向模式下播放视频,而 UI 的其余部分在纵向模式下应该保持不变,我该怎么办?
  • isShowingLandscapeView 是 UIWebview 还是其他视图?
  • 同样,我已经能够将我的 View Controller 设置为横向,但视频仍然以纵向启动:/
【解决方案2】:

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

别忘了添加这段代码

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

【讨论】:

  • 不,我只想保持纵向模式查看。但是现在,如果我保持纵向模式并运行视频,我将无法仅在播放器上进行旋转。这就是我想做的,以横向模式观看视频,但保持纵向模式。我只是想知道这是否可能。
  • @Anas10 对于那件事不要使用代码,即 shouldAutorotate ...只需将视图的属性更改为横向(如我上面的答案中所述)然后当您看到视频时,视图将只能在横向模式下加载。
  • 不行,我终于调整了我的视图,让它支持横向模式。
  • 请注意,“shouldAutorotateToInterfaceOrientation”在 iOS 6+ 中变为“shouldAutorotate”
  • 这在我的情况下不起作用 - 我有一个仅设置为横向的视图控制器,并且视频仍然仅以纵向模式启动...想法?
【解决方案3】:
-(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;
}}

//这适用于ios8

【讨论】:

    猜你喜欢
    • 2013-03-23
    • 1970-01-01
    • 1970-01-01
    • 2011-08-07
    • 2012-11-03
    • 2011-09-08
    • 2013-12-17
    • 2011-11-24
    • 1970-01-01
    相关资源
    最近更新 更多