如何将视图初始化为界面方向?
问题是我正在启动应用程序内电子邮件并触发键盘(通过 UITextFields),并且它们在我的横向应用程序上以纵向显示。我可以纵向旋转它,然后再次横向旋转它,观察应用程序从纵向旋转到横向时发生的黑色旋转角动画。由于我已经在 shouldAutorotateToInterfaceOrientation() 方法中锁定了除横向以外的任何方向,因此视图本身不会旋转,这很好。但是,当我添加这些视图时,应用程序会一直认为它们是纵向的,正如 UITextField 键盘和应用程序内电子邮件方向所证明的那样,除非我旋转到纵向,然后返回到横向(这会触发自动旋转方法返回 true)。
任何建议关于如何让这个应用程序知道当我添加视图时它已经处于横向状态?
更新:我曾尝试设置状态栏方向,但它对我不起作用,我不知道是否有什么问题。我删除视图然后向应用程序添加视图这一事实混淆了它对方向的理解。您知道 UITextField 和应用程序内邮件等 Apple 对象如何确定要显示的方向吗?对我来说奇怪的是,当我删除并添加新视图时,我倾斜手机以更改为横向的这些苹果小部件突然恢复为显示为纵向,即以下事件的一般顺序:
1)应用程序。横向,触发键盘,则显示纵向
2)倾斜装置,使其进入横向。关闭并打开键盘,它处于横向状态。
3)删除和添加一些视图。
4)再次触发键盘,竖屏显示
The problem is that I am launching in-app email and triggering keyboards (via UITextFields) and they are coming up portrait on my landscape app. I am able to rotate it portrait, then rotate it landscape again, observing the black rotation corners animation that occurs when an app rotates from portrait to landscape. Since I've locked out any orientation but landscape right in my shouldAutorotateToInterfaceOrientation() method, the view itself doesn't rotate, which is great. However, when I add these views, the app keeps thinking they are portrait as is evidenced by the UITextField keyboard and in-app email orientation unless I rotate to portrait, then back to landscape (which triggers the autorotate method to return true.
Any suggestions on how I can make this app know it's already in landscape to begin with when I'm adding views?
Update: I had tried setting the status bar orientation, but it does not work for me. I don't know if there's something with the fact that I remove and then add views to the app that is confusing its understanding of the orientation. Do you know how apple objects such as the UITextField and in-app mail determine what orientation to show? Do they simply poll the sharedApplication orientation? The odd thing for me is that it seems like when I remove and add new views, these apple widgets that I've tilted the phone to change to landscape suddenly revert to showing as portrait. i.e. this general sequence of events:
1) app in landscape, trigger keyboard, it shows portrait
2) tilt device to make it go into landscape. close and open keyboard and it's in landscape.
3) remove and add some views.
4) trigger keyboard again, it shows up in portrait
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
[UIApplication setStatusBarOrientation:animated:] 应该给你你想要的。
[UIApplication setStatusBarOrientation:animated:] should give you what you want.
在项目 plist 中设置
初始界面方向
字段Setup
Initial interface orientation
field in Project plist有趣的是,我发现在启动时设置状态栏方向还不够。观察字段值,我注意到它在不同时间重置为纵向,例如取消应用内邮件返回时。我发现多次调用来设置状态栏似乎可以解决问题。感谢您的意见,姆贝汉。
Interestingly, I found that setting the status bar orientation at the launch was not enough. Watching the field value, I noticed it was resetting to portrait at various times, such as when returning from canceling an in-app mail. I found making multiple calls to set the status bar seems to address the problem. Thanks for the input, mbehan.
删除/添加视图不应改变状态栏方向;据我所知,默认为视图控制器的方向,除非您明确设置它(然后您必须小心,因为视图控制器会重置它)。您可以尝试使用 UIStatusBarStyleBlackTranslucent 并在 VC 上设置 UIViewController.wantsFullScreenLayout 来查看发生了什么。
(过去,我注意到当我在仅纵向视图的顶部直接向窗口添加视图时,它会变得混乱。但它似乎在所有其他情况下都有效......)
您的实现是什么shouldAutorotateToInterfaceOrientation:?
Removing/adding views shouldn't change the status bar orientation; as far as I know, that defaults to the view controller's orientation unless you set it explicitly (and then you have to be careful, because view controllers reset it). You can try using UIStatusBarStyleBlackTranslucent and setting UIViewController.wantsFullScreenLayout on your VCs to see what's going on.
(In the past, I've noticed it getting confused when I add a view to the window directly, on top of a portrait-only view. It seems to work in all other cases though...)
What's your implementation(s) of shouldAutorotateToInterfaceOrientation:?
试试这个:
和旋转方法;
}
try this:
and the rotation method;
}