iOS 应用程序 - 更改选项卡栏中的方向

发布于 2024-12-03 15:09:43 字数 208 浏览 1 评论 0原文

在我的应用程序中,我有一个带有 5 个按钮的选项卡栏。整个应用程序只有纵向方向,我已经正确设置了它。

我的问题是,当我单击选项卡栏的第二个按钮时,视图通常以纵向显示,但当用户倾斜设备时,我希望将该特定视图更改为横向。是否可以将选项卡栏位置更改为横向,并且当单击其他按钮时全部更改为纵向?

否则,当它倾斜时,我必须在没有选项卡栏的情况下单独以横向方式显示视图,我该怎么做?

In my app I have a tabbar with 5 buttons. The total app has only portrait orientation and I have set it correctly.

My problem is that when I click the second button of the tabbar, the view appears normally in portrait but when the user tilts the device I want that particular view to be get changed to landscape. Is it possible to change the tabbar position to landscape and when the other buttons are clicked all to be changed to portrait?

Else while it is tilted I have to show the view alone in landscape without the tabbar, how can I do this?

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

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

发布评论

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

评论(4

瞳孔里扚悲伤 2024-12-10 15:09:43

因此,需要明确的是,您希望整个应用程序和所有视图都处于纵向模式,除了由第二个选项卡栏按钮触发的视图(您希望始终以横向模式显示)?

如果我没有混淆你的问题,那么只需将以下代码行放入视图号 2 的控制器的“viewDidLoad”方法中

self.view.transform = CGAffineTransformMakeRotation(M_PI / 2);

So, to be clear, you want the entire app and all views in portrait EXCEPT for the view triggered by the second tab bar button, which you want to always appear in Landscape?

if I've not confused your question, than just put the following line of code in the "viewDidLoad" method of the controller of view number 2

self.view.transform = CGAffineTransformMakeRotation(M_PI / 2);
情话已封尘 2024-12-10 15:09:43

您可能想要硬着头皮让您的项目在最高级别接受多个方向,然后根据 UIViewController 决定支持哪些方向。这将使框架能够为您处理大量工作(总是一个好主意)。

在每个视图的控制器中,重写此方法:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

对于您希望视图支持的任何方向,您都需要返回 YES

interfaceOrientation 参数的四个选项是:

UIInterfaceOrientationPortrait
UIInterfaceOrientationPortraitUpsideDown
UIInterfaceOrientationLandscapeLeft
UIInterfaceOrientationLandscapeRight

因此,对于您的情况,您将希望托管视图和选项卡栏的视图控制器支持所有方向。该函数如下所示:

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return YES;
}

但对于额外的细节,如果您只想支持横向左和纵向颠倒方向,该函数将如下所示:

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}

You probably want to bite the bullet and let your project accept multiple orientations at its highest level and then decide per UIViewController which orientations are supported. This will allow the frameworks to take care of a lot of the work for you (always a good idea).

In each view's controller, override this method:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

You will want to return YES for any orientation that you want the view to support.

The four options for the interfaceOrientation parameter are:

UIInterfaceOrientationPortrait
UIInterfaceOrientationPortraitUpsideDown
UIInterfaceOrientationLandscapeLeft
UIInterfaceOrientationLandscapeRight

So for your case, you will want the view controller hosting your view and tab bar to support all of the orientations. This is how the function would look:

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return YES;
}

But for a extra detail, if you wanted to support only Landscape Left and Portrait Upside Down orientation, the function would look like this:

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}
白衬杉格子梦 2024-12-10 15:09:43

如果所有选项卡的 ViewController 在 shouldAutorotateToInterfaceOrientation 处返回 YES,则 UITabBar 仅支持旋转。

如果您让“肖像”选项卡检查它们是否可见(通过

tabbar.selectedViewController 


tabbar.selectedIndex

并且仅在未选择时回复 yes。

但是,当用户在横向模式下更改选项卡时,用户体验可能会令人困惑
呈现肖像视图...

A UITabBar only supports rotation, if the ViewControllers of all tabs return YES at shouldAutorotateToInterfaceOrientation.

You could achieve what you want, if you let your "portrait" tabs check if they are visible (via

tabbar.selectedViewController 

or
tabbar.selectedIndex

and only reply yes when they're not selected.

The user experience could be confusing though, when the user changes the tab in landscape mode
a portrait view is presented ...

旧情勿念 2024-12-10 15:09:43

试试这个: tabbar.selectedViewController

[[UIDevice currentDevice] setOrientation:UIDeviceOrientationLandscapeRight];

try this w.r.t.: tabbar.selectedViewController

[[UIDevice currentDevice] setOrientation:UIDeviceOrientationLandscapeRight];

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