iphone - UIWebView 中横向模式的 Youtube 视频

标签 iphone objective-c ios xcode uiwebview

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

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

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

最佳答案

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

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

关于iphone - UIWebView 中横向模式的 Youtube 视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8111207/

相关文章:

ios - Apple 推送通知无法通过部署在 Linux 中的 Java Web 服务运行

ios - 如何构造多个复杂条件的NSPredicate

ios - 如何在开发中测试 Apple Universal Links?

iphone - 由于 "Application exited abnormally with signal 9"或“应用程序异常退出,信号 11 : Segmentation fault"in Iphone 导致应用程序崩溃

ios - NSNetService 和 GCDAsynSocket 有什么区别?

objective-c - 如何在没有额外自定义代码的情况下强制使正在运行的 Cocoa 程序失效?

iOS UITableView 选择隐藏 subview 按钮背景色

ios - NSInvalidArgumentException 使用 UISegmentedControl

iphone - 在 MKMapView 上获取用户当前位置

iphone - 如何以模态方式呈现标准的 UIViewController