我试图以纵向方式保持 UITableViewcontroller 。因此,我不想旋转到横向模式。我在下面添加了方法。但它并没有帮助,请注意我使用的是iOS 8:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if(interfaceOrientation== UIInterfaceOrientationPortrait)
{
return YES;
}else
{
return NO;
}
}
注意:我通过 UINavigationController
调用UITableViewUINavigationController *navigationController = [[UINavigationController alloc]
initWithRootViewController:svc];
// configure the new view controller explicitly here.
[self presentViewController:navigationController animated:YES completion: nil];
答案 0 :(得分:2)
shouldAutorotateToInterfaceOrientation:
已被弃用。您应该使用supportedInterfaceOrientations
和shouldAutorotate
。
以下是您的工作方式:
- (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];
}
请注意,您无法强制设备方向,因此如果应用程序处于横向状态,然后您按下表格视图控制器,它仍将处于横向状态。有很多方法可以解决这个问题:
答案 1 :(得分:1)
shouldAutorotateToInterfaceOrientation:已弃用。相反,使用:
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotate
{
return NO;
}