我希望在全屏模式下横向播放视频,但我的应用程序被锁定在竖屏模式下。如何实现这一点?请帮助我。谢谢。
我希望在全屏模式下横向播放视频,但我的应用程序被锁定在竖屏模式下。如何实现这一点?请帮助我。谢谢。
Swift 3 中最简单的解决方案,请将以下内容添加到您的应用程序委托中:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if window == self.window {
return .portrait
} else {
return .allButUpsideDown
}
}
我在我的一个应用程序中做过这个。要做到这一点,您需要检查您的视图控制器是否是您想播放视频的位置。
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;
}
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
}
就这样,伙计们。
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;
}
videoPlayerViewController
自动旋转,并将该videoplayerviewcontroller
的方向设置为landscapeleft
。 - Ketan Parmar将此文件添加到您的项目中
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)
它会像魔术一样运行