我已完成我的iOS应用程序,但我需要将仅一个视图设置为横向模式,其余视图只能在纵向模式下查看。
我使用的是Xcode 5.1,并通过从右侧面板拖放我的storyboard视图控制器来创建所有视图,因此,如果您要告诉我在哪里编写一些代码,请告诉我需要在哪里编写。
我在这里阅读了一个解决方案 UINavigationController Force Rotate ,但我不知道在哪里编写那段代码。我需要手动创建一个UIViewController
吗?
我已完成我的iOS应用程序,但我需要将仅一个视图设置为横向模式,其余视图只能在纵向模式下查看。
我使用的是Xcode 5.1,并通过从右侧面板拖放我的storyboard视图控制器来创建所有视图,因此,如果您要告诉我在哪里编写一些代码,请告诉我需要在哪里编写。
我在这里阅读了一个解决方案 UINavigationController Force Rotate ,但我不知道在哪里编写那段代码。我需要手动创建一个UIViewController
吗?
Swift
AppDelegate.swift
internal var shouldRotate = false
func application(_ application: UIApplication,
supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return shouldRotate ? .allButUpsideDown : .portrait
}
您的景观视图控制器
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.shouldRotate = true // or false to disable rotation
Objective-C
AppDelegate.h
@property (assign, nonatomic) BOOL shouldRotate;
AppDelegate.m
- (UIInterfaceOrientationMask)application:(UIApplication *)application
supportedInterfaceOrientationsForWindow:(UIWindow *)window {
return self.shouldRotate ? UIInterfaceOrientationMaskAllButUpsideDown
: UIInterfaceOrientationMaskPortrait;
}
您的横屏视图控制器
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[appDelegate setShouldRotate:YES]; // or NO to disable rotation
我假设你的目标是iOS 7(使用XCode 5.1,我认为我是正确的)。
首先,您必须了解,为了在横向模式下打开40多个视图中的一个视图,您的应用程序应允许横向和纵向界面方向。
这是默认情况,但您可以在目标的设置中检查,即常规
选项卡,部署信息
部分(请参见下面的屏幕截图)。
然后,因为您允许整个应用程序既支持横向又支持纵向,所以您将不得不告诉每个仅支持纵向的 UIViewController
不要自动旋转,并添加此方法的实现:
- (BOOL)shouldAutorotate {
return NO;
}
最后,针对您的特定横向控制器,并且因为您表示您正在以模态方式呈现它,您只需实现这些方法:
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeLeft; // or Right of course
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
希望这能有所帮助,
XXXViewController.m
文件中编写第一个代码片段。如果您想让视图保持横屏,则应将第二个代码片段放在 YYYViewController.m
文件中。如果您只是将视图控制器拖入故事板中,则应将您的竖屏视图控制器连接到至少一个 UIViewController
子类(创建/新建/文件 + UIViewController 子类),并将您的横屏视图控制器连接到不同的 UIViewController
子类。 - ZedenemSWIFT4
将以下代码行添加到您的AppDelegate中
var orientationLock = UIInterfaceOrientationMask.portrait
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return self.orientationLock
}
struct AppUtility {
static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
if let delegate = UIApplication.shared.delegate as? AppDelegate {
delegate.orientationLock = orientation
}
}
static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) {
self.lockOrientation(orientation)
UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
}
}
在您想横向呈现的控制器中添加以下代码。override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
AppDelegate.AppUtility.lockOrientation(UIInterfaceOrientationMask.landscapeRight, andRotateTo: UIInterfaceOrientation.landscapeRight)
}
override func viewWillDisappear(_ animated : Bool) {
super.viewWillDisappear(animated)
AppDelegate.AppUtility.lockOrientation(UIInterfaceOrientationMask.portrait, andRotateTo: UIInterfaceOrientation.portrait)
}
为了跟进Yaroslav的解决方法,如果要允许旋转到只有一个视图的横屏模式,则应在横屏视图控制器的viewWillAppear方法中将shouldRotate设置为YES,并在viewWillDisappear中将其设置为NO。
如果您只在viewWillAppear中将setShouldRotate设置为YES,则退出此视图控制器后,所有其他视图控制器也会旋转。因此,您必须在viewWillDisappear中将setShouldRotate设置为NO,以限制纵向视图的旋转。
您应该将shouldRotate声明为internal,以消除非公共类中的公共属性警告。 internal var shouldRotate = false