2014-11-21 47 views
0

我试图保留UITableViewcontroller纵向。因此,我不想旋转到横向模式。我在下面添加了方法。但它并没有帮助,请注意,我使用的是iOS 8:禁用UITableViewcontroller从横向旋转(保持纵向)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    if(interfaceOrientation== UIInterfaceOrientationPortrait) 
    { 
     return YES; 
    }else 
    { 
     return NO; 
    } 

} 

注意:我通过调用的UITableView UINavigationController的

UINavigationController *navigationController = [[UINavigationController alloc] 
                initWithRootViewController:svc]; 
    // configure the new view controller explicitly here. 




    [self presentViewController:navigationController animated:YES completion: nil]; 

回答

2

shouldAutorotateToInterfaceOrientation:以来的iOS 6.0已被弃用。您应该使用supportedInterfaceOrientationsshouldAutorotate

这里是你如何做到这一点:

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    return UIInterfaceOrientationPortrait; 
} 

- (NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskPortrait; 
} 

- (BOOL)shouldAutorotate 
{ 
    return NO; 
} 

编辑 - 为UINavigationController

这是一个可能的方式做到这一点:

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    if ([self.visibleViewController isKindOfClass:[UITableViewController class]]) 
     return UIInterfaceOrientationPortrait; 
    else 
     return [super preferredInterfaceOrientationForPresentation]; 
} 

- (NSUInteger)supportedInterfaceOrientations 
{ 
    if ([self.visibleViewController isKindOfClass:[UITableViewController class]]) 
     return UIInterfaceOrientationMaskPortrait; 
    else 
     return [super supportedInterfaceOrientations]; 
} 

- (BOOL)shouldAutorotate 
{ 
    if ([self.visibleViewController isKindOfClass:[UITableViewController class]]) 
     return NO; 
    else 
     return [super shouldAutorotate]; 
} 

请注意,你不能强迫设备的方向,所以如果应用程序在横向,然后你推动表视图控制器,它仍然是横向。有很多方法可以解决这个问题:

  • 阻止用户打开表视图控制器,通过显示一个警告,要求他们先旋转设备。
  • 隐藏表格视图并显示带有消息(或其他指示符)的标签,以通知用户旋转其设备。
  • 处理两个方向。
+0

...谢谢,但它仍然在旋转......注意我正在使用故事板,这样做有什么区别。 – user836026 2014-11-21 17:31:46

+0

不,不应该这样做。在这些方法中添加断点以确保它们被调用。 – 2014-11-21 17:53:40

+1

哦,只是注意到你的'UINavigationController'的更新。当'visibleViewController'是你的表视图控制器时,你需要子类* that *并覆盖这些方法以返回'portrait'。 – 2014-11-21 17:56:15

1

shouldAutorotateToInterfaceOrientation:depricated。相反,使用:

- (NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskPortrait; 
} 

- (BOOL)shouldAutorotate 
{ 
    return NO; 
} 
相关问题