【问题标题】:Auto Rotate Dynamic UIViewcontroller自动旋转动态 UIView 控制器
【发布时间】:2013-05-02 08:50:37
【问题描述】:

我有一个场景,我正在动态创建UIViewcontrollers 并在其顶部添加一个 UIView 并将其推送到导航堆栈。这就是我创建 view Controller

的方式
UIViewController *vc = [UIViewController new];
[vc.view addSubView:customView];
[self.navigationController pushViewController vc];

在设备上运行我的应用程序时,我的视图不会自动旋转。 (如果 VC 有一个实现文件,我会在 shouldAutorotate 中返回 YES 以使其工作。) 任何指针/帮助表示赞赏。

根据乔治的回复编辑:

George 的代码完美适用于 ios6 及更高版本。 supportedInterfaceOrientations API 已为 ios6.0+ 提供,正在寻找 ios4.3+ 的一般修复 谢谢..

【问题讨论】:

标签: iphone ios objective-c ipad uiviewcontroller


【解决方案1】:

如果您的导航控制器是 rootviewcontroller,则将此类别添加到您创建它的位置,如果添加到必须旋转的 viewcontroller 没有帮助

@implementation UINavigationController(Rotate)

-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;    
}

@end

也可能是您忘记在您的应用委托中添加

- (NSUInteger)supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return UIInterfaceOrientationMaskAll;
}

编辑:

对于 iOS 5,您只需将此方法添加到所需的视图控制器。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

【讨论】:

  • 感谢乔治的回复,我已经编辑了我的问题。你的建议确实奏效了。但它不适用于 ios 5
  • 你应该在你的所有视图控制器中添加这个方法——不仅仅是容器视图控制器。 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return YES; }
【解决方案2】:

在你的视图控制器中试试这个委托

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {

return YES;
}

【讨论】:

  • Kiran,如果你创建一个继承 UIViewController 的文件,你可以在 .m 文件中看到只需要实现的委托(你也可以看到默认注释的“shouldAutorotateToInterfaceOrientation”方法)。默认情况下,接口方向委托方法未实现,因为它们是可选的。在这里,您正在创建 UIViewController 的实例,因为这些委托没有实现,所以您的方向更改不会反映在其中。希望现在对你来说很清楚:)
  • 是的奥古斯丁,我清楚地理解这个事实,我正在创建一个 UIViewController 的实例并且它的委托没有实现。有没有办法在不创建 .h 和 .m 文件的情况下实现它?
  • Kiran,我不明白你为什么只是创建一个 UIViewController 的实例。如果不继承 UIViewController,您将无法实现 autoRotate 委托。所以我认为如果你不处理它,autoRotation可能无法正常工作。
猜你喜欢
  • 2011-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多