iOS 应用程序 - 更改选项卡栏中的方向
在我的应用程序中,我有一个带有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
因此,需要明确的是,您希望整个应用程序和所有视图都处于纵向模式,除了由第二个选项卡栏按钮触发的视图(您希望始终以横向模式显示)?
如果我没有混淆你的问题,那么只需将以下代码行放入视图号 2 的控制器的“viewDidLoad”方法中
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
您可能想要硬着头皮让您的项目在最高级别接受多个方向,然后根据 UIViewController 决定支持哪些方向。这将使框架能够为您处理大量工作(总是一个好主意)。
在每个视图的控制器中,重写此方法:
对于您希望视图支持的任何方向,您都需要返回
YES
。interfaceOrientation 参数的四个选项是:
因此,对于您的情况,您将希望托管视图和选项卡栏的视图控制器支持所有方向。该函数如下所示:
但对于额外的细节,如果您只想支持横向左和纵向颠倒方向,该函数将如下所示:
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:
You will want to return
YES
for any orientation that you want the view to support.The four options for the interfaceOrientation parameter are:
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:
But for a extra detail, if you wanted to support only Landscape Left and Portrait Upside Down orientation, the function would look like this:
如果所有选项卡的 ViewController 在 shouldAutorotateToInterfaceOrientation 处返回 YES,则 UITabBar 仅支持旋转。
如果您让“肖像”选项卡检查它们是否可见(通过
或
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
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 ...
试试这个:
tabbar.selectedViewController
[[UIDevice currentDevice] setOrientation:UIDeviceOrientationLandscapeRight];
try this w.r.t.:
tabbar.selectedViewController
[[UIDevice currentDevice] setOrientation:UIDeviceOrientationLandscapeRight];