【问题标题】:Programmatically change orientation of ViewController without subclassing UINavigationController在不继承 UINavigationController 的情况下以编程方式更改 ViewController 的方向
【发布时间】:2016-05-16 13:24:44
【问题描述】:

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

【问题讨论】:

  • VC 的方向与 UINavigationController 有什么关系?
  • 据我了解...如果我在 NavigationController 上更改 VC 的方向,我应该继承 NavigationController,并覆盖 supportedInterfaceOrientationsshouldAutorotate

标签: ios objective-c iphone uiviewcontroller orientation


【解决方案1】:

在 ViewController 的 viewDidAppear 方法中设置:

对于风景:

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

纵向:

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

【讨论】:

  • 不走运。事实上,我也需要切换按钮来改变方向。
【解决方案2】:

UINavigationController 无关紧要。 supportedInterfaceOrientationsshouldAutorotate 没有被调用,因为 iOS 9 (iPad) 阻止了它们。我可以在(目标设置)-常规-部署信息上检查需要全屏来解决这个问题。

这是我发现的(详细)。 https://stackoverflow.com/a/33567990/1010443

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-02
    • 1970-01-01
    • 1970-01-01
    • 2015-02-08
    • 1970-01-01
    • 2021-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多