在我的项目中,我只允许纵向旋转,但对于一个ViewController
,我想启用横向。我将此ViewController
呈现为ModalViewController
,我尝试使用方法- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation
或iOS 6方法,例如-(NSUInteger) supportedInterfaceOrientations
,但实际上没有任何效果。虽然这些方法被调用,但视图没有旋转。
在此之后,我试图通过听取这些通知来调整myslef:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didRotate:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
但即使我能够在方法view
中手动旋转didRotate
:它非常混乱,我无法旋转StatusBar
。
我真的很想使用像shouldAutorotateToInterfaceOrientation
这样的标准方法,但我不知道怎么做。任何人吗?
答案 0 :(得分:6)
在appate delegate.m
中添加此内容# pragma mark - Rotation
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if ([self.window.rootViewController isKindOfClass:[MVYSideMenuController class]]) {
// Get topmost/visible view controller
UIViewController *currentViewController = [self.window.rootViewController.childViewControllers lastObject];
// Check whether it implements a dummy methods called canRotate
if ([currentViewController respondsToSelector:@selector(canRotate)]) {
// Unlock landscape view orientations for this view controller
return UIInterfaceOrientationMaskAllButUpsideDown;
}
}
// Only allow portrait (standard behaviour)
return UIInterfaceOrientationMaskPortrait;
}
-(void)canRotate
{
}
然后添加此方法
-(void)canRotate
{
// just define the method, no code required here
}
在每个要提供旋转的ViewController(.m文件)中。您还可以在此处使用-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
方法在设备旋转时做出反应:
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
switch (orientation) {
case 1:
case 2:
//NSLog(@"portrait");
break;
case 3:
case 4:
//NSLog(@"landscape");
break;
default:
//NSLog(@"other");
break;
}
}
答案 1 :(得分:2)
为需要旋转的屏幕子类化导航控制器。
在.m
// Older versions of iOS (deprecated) if supporting iOS < 5
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}
// iOS6
- (BOOL)shouldAutorotate {
return YES;
}
// iOS6
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
这将覆盖iOS 6摘要页面中设置的旋转方法。
在iOS 6中,视图控制器仅查找父级或根控制器的旋转方法
答案 2 :(得分:0)
你只能在viewDidLoad和viewWillAppear中调用shouldAutoRotateToInterfaceOrientation,如下所示:
[self shouldAutorotateToInterfaceOrientation];
应该在ViewController中调用方法
答案 3 :(得分:-1)
实现在所有控制器中并返回特定控制器所需的interfaceOrientation
For All
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation{
return YES;
}
景观
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight));
}
For Portrait
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return ((interfaceOrientation == UIInterfaceOrientationPortrait) || (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown));
}