[英]iOS: How to change Orientation of only one view controller in app?
我在应用程序中的视图方向遇到问题。
喜欢
我有两个视图控制器,VC1和VC2
VC1具有固定的横向方向。 VC2兼有
VC1 - > VC2很好。 这意味着当我从VC1转到VC2时,VC2会同时改变其横向和纵向方向。
但是,当我从VC2(其中VC2为纵向模式)恢复到VC1时,VC1也处于纵向模式,但是我希望VC1仅处于横向模式,而与VC2模式无关。
请大家帮帮我 寻求最近2天的解决方案。 提前致谢。
请参阅下面的链接以获取解
http://swiftiostutorials.com/ios-orientations-landscape-orientation-one-view-controller/
在您的VC1中。
-(BOOL)shouldAutorotate
{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeLeft;
}
希望它能帮到你......
首先,在AppDelegate.m中编写
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return (UIInterfaceOrientationMaskAll);
}
然后,对于VC1(横向),编写以下代码:
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMaskPortrait);
}
对于同时具有两者的VC2,请将掩码更改为全部。
- (NSUInteger)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMaskAllButUpsideDown);
//OR return (UIInterfaceOrientationMaskAll);
}
基本上iOS应用程序中基于视图控制器的方向支持是小菜一碟 - 我在Swift中完成了代码,但是ObjC概念与此完全相同。
我已经使用UINavigationController
创建了一个快速教程,但您可以通过保留概念但稍微更新环境将其转换为UITabBarController
。
最初为您的应用设置方向支持,如下所示:
创建一个基于UINavigationController
的简单故事板,如下所示:
注意: 如果它最初没有说OrientaionSupporterController
请不要担心(如果你在这里发现一致错误,则OrientaionSupporterController
奖励!),我将在下一步中介绍它。
创建导航控制器的子集,如下所示:
import UIKit
// MARK: - Implementation
class OrientaionSupporterController: UINavigationController {
// MARK: - Active View Controller
private var activeViewController: UIViewController? {
get {
return self.presentedViewController ?? self.topViewController // for possible modal support
}
}
// MARK: - Orientations
override var shouldAutorotate: Bool {
get {
return self.activeViewController?.shouldAutorotate ?? true // yes, rotate it, please
}
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return self.activeViewController?.supportedInterfaceOrientations ?? .all // default is all possible orientations
}
}
然后确保它成为IB中的基类:
创建具有自定义方向支持的各个视图控制器,例如仅支持纵向和横向左支持:
import UIKit
// MARK: - Implementation
class ViewController: UIViewController {
// MARK: - Orientation
override var shouldAutorotate: Bool {
get {
return true
}
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return [.portrait, .landscapeLeft] // no one find that preactical but works flawlessly
}
}
注意: 您可以随时使用UITabBarController
转换本教程,您只需(显然)创建tab-bar-controller的子集并使用selectedViewController
获取当前可见的一个。
注意#2: 很明显,您可以通过这种方式更进一步,您可以在视图层次结构中嵌套自定义导航控制器,或者如果队列中有多个UIWindow
实例,并覆盖每个窗口中支持的方向(例如,一些支持所有四个方向,而一些其他窗口仅支持例如视频播放,等等...)
1.启用项目的所有方向支持。
对于VC1,添加此行VC1Controller类
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
对于VC2在VC2Controller类中添加它
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.