在我的应用程序被锁定为竖屏模式的情况下,如何使视频全屏播放并且横屏显示?

5

我希望在全屏模式下横向播放视频,但我的应用程序被锁定在竖屏模式下。如何实现这一点?请帮助我。谢谢。

5个回答

10

Swift 3 中最简单的解决方案,请将以下内容添加到您的应用程序委托中:

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    if window == self.window {
        return .portrait
    } else {
        return .allButUpsideDown
    }
}

2
哥们,你真棒!简单而优雅的解决方案。你救了我的一天。谢谢你。 - rmvz3
这太棒了!但我遇到了一个奇怪的 bug。如果用户在横屏状态下点击视频完成,导航栏会对所有视图控制器向上移动30像素左右。有什么解决方法吗? - Groovietunes
1
我已经发布了我的答案上面(已接受:P),但现在我使用了类似于你的新方法,但是在ObjC中。 - Pratik Jamariya
@Groovietunes,我在我的一个应用程序中遇到了与你类似的问题,你能告诉我你是如何关闭/移除你的播放器窗口的吗?我可以帮你解决这个问题。 - Pratik Jamariya
1
@Eyzuky 很好的观点。基本思路很不错。详细的逻辑来检查是否是正确的窗口可能因不同的应用程序而异。谢谢! - JonSlowCN
显示剩余4条评论

2

我在我的一个应用程序中做过这个。要做到这一点,您需要检查您的视图控制器是否是您想播放视频的位置。

  • The first thing you have to do is check Device Orientation to Portrait,Landscape left, Landscape right in your project target

  • In your AppDelegate do the below code

    -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    
        UIStoryboard *mainStoryboard;
        if (IS_IPHONE) {
            mainStoryboard= [UIStoryboard storyboardWithName:@"Main" bundle: nil];
        }
        else{
           mainStoryboard= [UIStoryboard storyboardWithName:@"Main_iPad" bundle: nil];
        }
    
        ViewControllerThatPlaysVideo *currentViewController=    ViewControllerThatPlaysVideo*)[mainStoryboard     instantiateViewControllerWithIdentifier:@"postDetailView"];
        if(navigationController.visibleViewController isKindOfClass:    [ViewControllerThatPlaysVideo class]]){
           if([currentViewController playerState])
                return UIInterfaceOrientationMaskLandscape|UIInterfaceOrientationMaskPortrait;
           return UIInterfaceOrientationMaskPortrait;
        }
        return UIInterfaceOrientationMaskPortrait;
    }
    

这一个是完美的答案。 - Hemang
1
@Pratik Jamariya:非常感谢你。你帮我节省了很多时间。 - Monika Patel
你在上面提到的代码中所指的 playerstate 是什么?是关于视频播放器状态的吗? - Pradeep Reddy Kypa
@PradeepReddyKypa 是的,我在指视频播放器状态,无论是播放中、暂停、停止、缓冲等等。 - Pratik Jamariya
我可以在AppDelegate中调用navigationController。 - kemdo

1
在几乎整整一天盯着这个页面后,我终于找到了适用于所有人的省时解决方案,
前往项目导航器,选择您的项目,并在“常规”中仅选择设备方向中的竖屏
还要确保您正在使用模态转换来显示landscapeController。
现在在您想要横屏模式的控制器中添加此内容。
override func viewWillAppear(_ animated: Bool) {

    super.viewWillAppear(true)
}

override var shouldAutorotate: Bool {

    return false
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {

    return .landscapeRight
}

override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {

    return .landscapeRight
}

就这样,伙计们。


1
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];

只需在呈现的视图控制器的- viewDidAppear:中调用它。

另一种方法:

首先,您必须了解,为了在横向模式下打开超过100个视图中的一个,您的应用程序应允许横向和纵向界面方向。这是默认情况,但您可以在目标设置的“常规”选项卡中的“部署信息”部分中检查它。

然后,因为您允许整个应用程序既支持横向又支持纵向,所以您将不得不告诉每个仅支持纵向的UIViewController它不应该自动旋转,添加此方法的实现:

- (BOOL)shouldAutorotate {
    return NO;
}

最后,针对你的特定横屏控制器,并且因为你说你是以模态方式呈现它,你可以直接实现这些方法:-
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationLandscapeLeft; // or Right of course
}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

希望这能帮到你 :)

我的视图旋转得很完美,但视频在横向模式下无法播放。请查看我的截图。@Lion - Monika Patel
那个视图控制器根据你的截图没有在横向旋转。所以请尝试我的答案。允许videoPlayerViewController自动旋转,并将该videoplayerviewcontroller的方向设置为landscapeleft - Ketan Parmar

0

将此文件添加到您的项目中

import UIKit

final class DeviceOrientation {
    
    static let shared: DeviceOrientation = DeviceOrientation()
    
    // MARK: - Private methods
    
    @available(iOS 13.0, *)
    private var windowScene: UIWindowScene? {
        return UIApplication.shared.connectedScenes.first as? UIWindowScene
    }
    
    // MARK: - Public methods
    
    func set(orientation: UIInterfaceOrientationMask) {
        if #available(iOS 16.0, *) {
            windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: orientation))
        } else {
            UIDevice.current.setValue(orientation.toUIInterfaceOrientation.rawValue, forKey: "orientation")
        }
    }
    
    var isLandscape: Bool {
        if #available(iOS 16.0, *) {
            return windowScene?.interfaceOrientation.isLandscape ?? false
        }
        return UIDevice.current.orientation.isLandscape
    }
    
    var isPortrait: Bool {
        if #available(iOS 16.0, *) {
            return windowScene?.interfaceOrientation.isPortrait ?? false
        }
        return UIDevice.current.orientation.isPortrait
    }
    
    var isFlat: Bool {
        if #available(iOS 16.0, *) {
            return false
        }
        return UIDevice.current.orientation.isFlat
    }
}

extension UIInterfaceOrientationMask {
    var toUIInterfaceOrientation: UIInterfaceOrientation {
        switch self {
        case .portrait:
            return UIInterfaceOrientation.portrait
        case .portraitUpsideDown:
            return UIInterfaceOrientation.portraitUpsideDown
        case .landscapeRight:
            return UIInterfaceOrientation.landscapeRight
        case .landscapeLeft:
            return UIInterfaceOrientation.landscapeLeft
        default:
            return UIInterfaceOrientation.unknown
        }
    }
}

我们如何使用它?

DeviceOrientation.shared.set(orientation: .landscapeRight) 

它会像魔术一样运行


这段代码假设运行的应用程序只有一个场景。这意味着这段代码不支持在iPad上进行多任务处理。 - HangarRash
我在 iPhone 上测试了这段代码,它可以正常工作,但我没有在 iPad 上测试过。所以我不知道会不会成功。 - Ahmed Rajib

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接