在UIWebView中的横向模式的Youtubevideo

我的应用程序不是为了风景。 但是,当我在UIWebView中打开我的YouTube频道并且用户启动video时 ,它将显示在肖像中。 如果用户旋转他的iPhone,我想使其以横向模式出现。

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

我知道有这样的“肮脏的黑客”,但我更喜欢更清洁的东西。 另外我不希望UIWebView切换到风景,但只是video可以。

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

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

要在横向视图中显示video,只需转到在Xcode中实现了webView的视图的属性检查器,然后将方向从纵向更改为横向。 尽可能简单

不要忘记添加这个代码

 -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) { return YES; } } 
 -(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