mae*_*aeq 37 xcode landscape objective-c viewcontroller ios
我完成了我的iOS应用程序,但我只需要将一个视图设置为横向模式,其余视图只能在纵向模式下查看.
我正在使用Xcode 5.1,我通过从右侧面板中删除我的故事板视图控制器来创建我的所有视图,所以如果你要告诉我在某处编写一些代码,请告诉我我需要编写它的确切位置.
我在这里阅读了一个解决方案UINavigationController Force Rotate但我不知道在哪里编写该代码.我需要UIViewController手动创建一个吗?
Yar*_*lav 89
迅速
AppDelegate.swift
internal var shouldRotate = false
func application(_ application: UIApplication,
supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return shouldRotate ? .allButUpsideDown : .portrait
}
Run Code Online (Sandbox Code Playgroud)
您的横向视图控制器
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.shouldRotate = true // or false to disable rotation
Run Code Online (Sandbox Code Playgroud)
Objective-C的
AppDelegate.h
@property (assign, nonatomic) BOOL shouldRotate;
AppDelegate.m
- (UIInterfaceOrientationMask)application:(UIApplication *)application
supportedInterfaceOrientationsForWindow:(UIWindow *)window {
return self.shouldRotate ? UIInterfaceOrientationMaskAllButUpsideDown
: UIInterfaceOrientationMaskPortrait;
}
Run Code Online (Sandbox Code Playgroud)
您的横向视图控制器
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[appDelegate setShouldRotate:YES]; // or NO to disable rotation
Run Code Online (Sandbox Code Playgroud)
Zed*_*nem 38
我假设你在这里瞄准iOS 7(使用XCode 5.1,我认为我是对的).
首先,您必须了解,为了打开40多个景观中的一个视图,您的应用应该允许横向和纵向界面方向.默认情况下是这种情况,但您可以在目标设置,General标签,Deployment Info部分中查看它(请参见下面的屏幕截图).

然后,因为您允许整个应用程序使用横向和纵向,所以您必须告诉每个只有纵向UIViewController它不应该自动旋转,添加此方法的实现:
- (BOOL)shouldAutorotate {
return NO;
}
Run Code Online (Sandbox Code Playgroud)
最后,对于您的特定横向控制器,并且因为您说您以模态方式呈现它,您可以实现这些方法:
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeLeft; // or Right of course
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
Run Code Online (Sandbox Code Playgroud)
希望这会有所帮助,
要跟进Yaroslav的解决方案,为了允许仅在一个视图中旋转到横向模式,横向视图控制器应在其viewWillAppear方法中将shouldRotate设置为YES,并在其viewWillDisappear中设置为NO.
如果在viewWillAppear中仅将ShowShouldRotate设置为YES,则在现有此视图控制器之后,所有其他视图控制器也将被旋转.因此,您必须在其viewWillDisappear中将SetShouldRotate设置为NO以限制纵向视图的旋转.
小智 6
SWIFT4
将以下代码行添加到您的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")
}
}
Run Code Online (Sandbox Code Playgroud)
将下面的代码添加到您想要的控制器中
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)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
41825 次 |
| 最近记录: |