<分区>
<分区>
在我的 iPhone 应用程序中,我已将支持的方向标记为如下屏幕截图。
但是我想阻止一些关于自动旋转的 View ,因为我正在使用下面的代码(ios6)
编辑
-(BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation
{
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
- (BOOL)shouldAutorotate
{
return NO;
}
但是 View 还在旋转,请帮我解决这个问题?
最佳答案
iOS 6.0 引入了新的旋转方法并且不调用 shouldAutorotateToInterfaceOrientation:
。
您需要实现这些方法以同时支持 iOS 6.0 之前的版本和 iOS 6.0+:
// Do as many checks as you want to allow for other orientations here for pre-iOS 6
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
// You can return YES here as this still checks supportedInterfaceOrientations
// before rotating, or you can return NO to 'lock' the view in whatever it's in...
// Making sure to return the appropriate value within supportedInterfaceOrientations
- (BOOL)shouldAutorotate
{
return YES;
}
// return supported orientations, bitwised OR-ed together
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
编辑 - 如果您的 UIViewController
在 UINavigationController
的层次结构中
如果您的 View Controller 在 UINavigationController
的 View 层次结构中,则旋转方法实际上是在导航 Controller (而不是 View Controller )上调用的...这由 Apple 在我的意见,但是您可以执行以下操作以允许 View Controller 改为响应这些方法-
在 UINavigationController
上创建一个类别,方法如下:
// UINavigationController+Additions.h file
@interface UINavigationController (Additions)
@end
// UINavigationController+Additions.m file
#import "UINavigationController+Additions.h"
@implementation UINavigationController (Additions)
- (BOOL)shouldAutorotate
{
return YES;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
UIViewController *viewController = [self topViewController];
if ([viewController respondsToSelector:@selector(shouldAutorotateToInterfaceOrientation:)])
{
return [viewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
}
return [super shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
}
- (NSUInteger)supportedInterfaceOrientations
{
UIViewController *viewController = [self topViewController];
if ([viewController respondsToSelector:@selector(supportedInterfaceOrientations)])
{
return [viewController supportedInterfaceOrientations];
}
return [super supportedInterfaceOrientations];
}
@end
编辑 2 - 如果您的 UIViewController
在 UITabBarController
的选项卡中
如果您的 View Controller 位于 UITabBarController
的选项卡内,则会出现同样的问题。
再次在 UITabBarController
上创建一个类别以允许 View Controller 响应:
// UITabBarController+Additions.h file
@interface UITabBarController (Additions)
@end
// UITabBarController+Additions.m file
#import "UITabBarController+Additions.h"
@implementation UITabBarController (Additions)
- (BOOL)shouldAutorotate
{
return YES;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
UIViewController *selectedViewController = [self selectedViewController];
if ([selectedViewController respondsToSelector:@selector(shouldAutorotateToInterfaceOrientation:)])
{
return [selectedViewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
}
return [super shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
}
- (NSUInteger)supportedInterfaceOrientations
{
UIViewController *selectedViewController = [self selectedViewController];
if ([selectedViewController respondsToSelector:@selector(supportedInterfaceOrientations)])
{
return [selectedViewController supportedInterfaceOrientations];
}
return [super supportedInterfaceOrientations];
}
@end
编辑 3
这里还有一些关于 Objective-C 类别的链接:
http://macdevelopertips.com/objective-c/objective-c-categories.html
http://mobile.tutsplus.com/tutorials/iphone/objective-c-categories/
关于iPhone 界面方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15081744/
相关文章:
iphone - 如何捕获 iPhone 应用程序中的其他应用程序事件?
iphone - 在纵向模式下从 iPhone 单击的图像会旋转 90 度
ios - swift 中的 AVPlayerViewController 和 AVPlayer 不显示任何内容
ios - 通过 WIFI 从 Iphone 到 Iphone 的企业应用程序分发(无 Web 服务器,无本地网络)
iphone - CustomTabBarController 不响应委托(delegate)回调
ios - UIView 自动布局 : hierarchy of subviews VS minimum views hierarchy?