2014-02-18 14 views
2

我在我的iPhone应用程序中使用带有多个UINavigationControllers的tabBarController,现在我想添加旋转支持(在某些视图控制器上,不是全部)。我知道我需要为我的UIViewControllers实现supportedInterfaceOrientation方法。UINavigationController supportedInterfaceOrientations:category vs swizzle

当设备旋转时,supportedInterfaceOrientation被称为在UINavigationController的,但我需要它在我的viewControllers调用。什么是最好的方法来做到这一点?

  • 我见过一些人创建的UINavigationController是一个大类,覆盖supportedInterfaceOrientation查询孩子viewControllers。但我知道苹果公司不愿意使用类别来覆盖方法。 (我已经实现了它这样的,它工作正常,但我想,以确保应用程序继续与iOS的未来版本中的工作。)有关使用方法混写

  • 什么?

  • 子类UINavigationController?

任何建议表示赞赏。 :)

回答

2

如果您使用的iOS 6+我建议继承UINavigationController和压倒一切的supportedInterfaceOrientations

类似的东西来:

- (NSUInteger)supportedInterfaceOrientations; 
{ 
    return [self.topViewController supportedInterfaceOrientations]; 
} 
2

你不需要任何类继承。只需添加通知观察员每类应该对定向反应改变

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenOrientationChanged) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil]; 
+0

虽然这种改变方向时,会给你一个通知,什么也不会做的是限制设备的某些方向的旋转这是什么OP要求。 –

+0

你说得对。但是我的变体更加灵活,所以它对作者也很有用。另外UINavigationController的子类将需要更多的代码来编写然后订阅和处理通知。 – Avt

相关问题