对所有 UIViewController 使用相同的 UIInterfaceOrientation

发布于 2024-12-28 17:26:46 字数 351 浏览 3 评论 0原文

现在添加到我的故事板中的任何新 UIViewController 都需要有一个包含以下代码的类:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

通过在每个类上复制/粘贴来维护很好,但这真的有必要吗?难道就没有更快的方法吗?

Right now any new UIViewController added to my storyboard needs to have a class that has the following code:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

Which is fine to maintain via copy/paste on each class, but is that really necessary? Is there not a faster way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愛上了 2025-01-04 17:26:46

您可以使用支持的界面方向键在 info.plist 文件中设置整个应用程序支持的界面方向(您也可以在项目摘要面板中以图形方式设置)。

如果这没有帮助(从下面的评论来看显然没有帮助),您可以将代码添加为 UIViewController 上的类别,从而使您不必将其复制并粘贴到每个控制器中。

如果这看起来有点激进,您可以创建一个包含该方法的 UIViewController 子类,并将其用作所有其他控制器的超类(BaseViewController 是一个很好的名称)。

You can set the supported interface orientations across the whole app in the info.plist file using the Supported interface orientations key (you can also set this graphically in the project summary panel).

If that doesn't help (which it apparently doesn't from your comment below) you could add your code as a category on UIViewController, thereby saving you from having to copy and paste it into every controller.

If that seems a bit radical, you could instead create a UIViewController subclass containing that method and use it as the superclass for all of your other controllers (BaseViewController would be a good name for it).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文