在UIWebView中横向模式的Youtube视频

ana*_*ali 12 iphone xcode objective-c uiwebview ios

我的应用程序不适用于景观.但是当我在UIWebView中打开我的YouTube频道并且用户启动视频时,它会显示在Portrait中.如果用户旋转他的iPhone,我想让它以横向模式显示.

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

我知道有"脏兮兮的黑客"这样做,但我更喜欢更干净的东西.此外,我不希望UIWebView切换到横向,但只是视频可以.

ana*_*ali 2

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

- (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));
}
Run Code Online (Sandbox Code Playgroud)

  • 不错的工作!但是你需要横向调整整个 UI 吗?如果我只想以横向模式播放视频,而 UI 的其余部分在纵向模式下保持不变,我该怎么办? (3认同)