ios - 在不继承 UINavigationController 的情况下以编程方式更改 ViewController 的方向

标签 ios objective-c iphone uiviewcontroller orientation

我正在编写一个包含 UIViewController(和 Storyboard)的模块,它可以被其他包装程序调用,所以我不能强制使用特定的 UINavigationController 的子类, 但它可以被 UINavigationController 使用。
在这种情况下,如何更改 ViewController 的方向?此外,只有一个 UIViewController 应该以编程方式更改方向。
我尝试使用 UINavigationControllerDelegate 但它没有被调用。 (我尝试在 initviewDidLoad 上设置委托(delegate),但都没有用)
没有其他方法可以解决这个问题吗?
任何帮助将不胜感激。

最佳答案

在 ViewController 的 viewDidAppear 方法中设置:

横向:

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

对于肖像:

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

关于ios - 在不继承 UINavigationController 的情况下以编程方式更改 ViewController 的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37255035/

相关文章:

ios - 使用带有 UIViewController 容器的 UISearchDisplayController

iphone - 过滤与核心数据的关系

iphone - 在核心图形中发布时遇到问题

ios - Google 登录未返回到我的应用

iphone - 是否可以将单词包装在 UITableViewSectionIndex 中?

ios - 在 iOS 中以编程方式生成 OpenGL 顶点数据

ios - iOS 7 的 UIToolbar 中 UIBarButtonItems 的垂直位置不正确

ios - Swift - 在不嵌入导航 Controller 的情况下从左到右的 Segue 过渡

ios - 了解 iOS 中的 View 层次结构

ios - 以框架的特定形状调整图像大小