在我的应用程序中,我启动了带有纵向导航 Controller 的第二个 View Controller ,然后在第四个 View Controller 中结束,我应该在其中旋转 iPhone 以显示图像。 现在的问题是: 如果在目标中我以这种方式设置方向:
我可以使用这种方法来旋转我的图像:
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
它工作正常,但我无法锁定属于导航 Controller 的所有 View ,因为它们都旋转纵向和横向。
第二个选项是以这种方式设置目标:
但是这个结构不检测方向:
在 View 中会出现:
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
[self willRotateToInterfaceOrientation:orientation duration:1];
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscape;
}
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
}
else
{
}
}
else{
if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
}
else
{
}
}
}
什么是最好的解决方案? 谢谢
最佳答案
我忍不住觉得,如果我阅读了文档,它会节省我一些时间,但对我来说,解决方案是子类 UINavigaionController 并添加以下内容:
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
而且因为这里真的没有任何实质性的事情发生,所以没有必要用 coder 或其他任何东西覆盖 init。您可以在之前引用导航 Controller 的任何地方引用这个子类,包括 Interface Builder。
关于IOS:在 UINavigationController 中锁定方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18334765/