gpt4 book ai didi

ios - 禁止 UITableViewcontroller 横向旋转(保持纵向)

转载 作者:行者123 更新时间:2023-11-28 19:54:52 25 4
gpt4 key购买 nike

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

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

}

注意:我通过 UINavigationController 调用 UITableView

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




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

最佳答案

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];
}

请注意,您不能强制设备方向,因此如果应用程序处于横向,然后您按下 TableView Controller ,它仍将处于横向。有多种方法可以处理此问题:

  • 通过显示要求用户先旋转设备的警告来阻止用户打开 TableView Controller 。
  • 隐藏表格 View 并显示带有消息(或其他一些指示符)的标签,告诉用户旋转他们的设备。
  • 处理两个方向。

关于ios - 禁止 UITableViewcontroller 横向旋转(保持纵向),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27066007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com