我试图按照方法在我的观点上强制横向显示:
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeLeft;
}
- (BOOL)shouldAutorotate{
return YES;
}
两者都没有奏效。请注意,我正在模拟器和iPad上进行测试。
由于
答案 0 :(得分:22)
以下是我使用NavigationViewController将我的一个视图强制为Landscape的方法:
View控制器中的导入消息:objc / message.h
在viewDidLoad方法中添加了以下代码:
objc_msgSend([UIDevice currentDevice],@ selector(setOrientation :),UIInterfaceOrientationLandscapeLeft);
希望它有所帮助。
答案 1 :(得分:7)
接受的答案似乎不适用于iOS 7.1.2。 (它在模拟器上工作,但在设备上运行时不起作用。)
这似乎有效:
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"];
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeLeft] forKey:@"orientation"];
答案 2 :(得分:1)
int shouldShowInLandscape = 0;
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(forceToLandscape:)
name:@"yourNameNotification"
object:nil];
}
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
return UIInterfaceOrientationMaskLandscapeLeft;
} else {
if(shouldShowInLandscape)
{
return UIInterfaceOrientationMaskLandscape;
}
return UIInterfaceOrientationMaskPortrait;
}
}
-(void) forceToLandscape:(NSNotification*) theNot
{
shouldShowInLandscape = [[theNot object] intValue];
}
//来自UIViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter]
postNotificationName:@"yourNameNotification"
object:[NSString stringWithFormat:@"1"]];
}
-(void) viewDidDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter]
postNotificationName:@"yourNameNotification"
object:[NSString stringWithFormat:@"0"]];
}
//删除notificationCenter