如何以编程方式设置设备(UI)方向?
希望屏幕(UI)上的所有内容都能够从横向左向右旋转,反之亦然。
我该怎么做呢?这是私人的吗?
我知道这
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {}
可以让你说出 UI 可以是哪个方向,但是有没有办法一次只强制一个方向?
干杯
Would like everything on the screen (UI) to be able to rotate from landscape left to right or vica versa.
How do I go about doing this? Is this private?
I know that
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {}
lets you say which orientations the UI can be, but is there a way to force only one at a time?
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
在 iOS 中,不存在
[[UIDevice currentDevice] setDeviceOrientation:UIDeviceOrientationSomeOrientation]
方法。但我们可以使用状态栏旋转视图,为此:In iOS
[[UIDevice currentDevice] setDeviceOrientation:UIDeviceOrientationSomeOrientation]
method is absent. But we can rotate a view with status bar, for this:我在这方面取得了成功:
I've had success with this:
调用该方法来确定您的界面是否应自动旋转到给定的旋转(即让 UIKit 完成艰苦的工作,而不是您手动完成)。
因此,如果您希望您的应用程序仅在横向模式下工作,您可以使用以下方法实现该方法的主体:
返回 UIInterfaceOrientationIsLandscape(interfaceOrientation);
如果您希望 UI 自动旋转到所有方向,您可以
<代码>
返回是;
这就是你问的吗?
That method is called to determine whether your interface should automatically rotate to a given rotation (i.e letting UIKit do the hard work, rather than you doing it manually).
So if you wanted your app to only work in landscape you'd implement the body of that method with:
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
If you wanted your UI to auto rotate to all orientations you could just
return YES;
Is that what you were asking?
如果您提供一个实现
-shouldAutorotateToInterfaceOrientation:
的模态视图控制器以仅支持一种方向,则整个界面将自动强制进入其中。我认为没有办法以编程方式改变方向。If you present a modal view controller which implements
-shouldAutorotateToInterfaceOrientation:
to only support one orientation, the whole interface will automatically be forced into it. I don't think there's a way to programmatically change orientation otherwise.这种简单的方法也有效:
然后返回:
Also this simple way works:
and back:
在 Swift 中将方向更改为纵向:
请参阅:https://stackoverflow.com/a/24259601
In Swift to change the orientation to portrait:
See: https://stackoverflow.com/a/24259601
解决者:Guntis Treulands https://stackoverflow.com/a/10146270/894671
This was addressed by: Guntis Treulands https://stackoverflow.com/a/10146270/894671
尽管这不是一个闲置的解决方案,但对我来说效果很好。
Even though this is not an idle solution, works well for me.
正如相关线程上的建议
shouldAutorotateToInterfaceOrientation
已弃用。较新的应用程序应利用 Apple 的 UIViewController 中描述的以下方法类引用,直到它们本身被弃用:shouldAutorotate
、preferredInterfaceOrientationForPresentation
;和attemptRotationToDeviceOrientation
。据我了解,最好对需要锁定(即具有首选)方向模式的视图使用不同的视图控制器,该模式与上一个不同。
As suggested on a related thread
shouldAutorotateToInterfaceOrientation
is deprecated. Newer apps should leverage the following methods as described in Apple's UIViewController Class Reference until they themselves become deprecated:shouldAutorotate
,preferredInterfaceOrientationForPresentation
; andattemptRotationToDeviceOrientation
.From what I understand it's best to use different View Controllers for views which need to lock into (i.e. have a preferred) orientation mode which differs from the last.