警报视图为纵向视图,iPad 为横向视图,并且无法旋转到除“其他”以外的任何视图。风景(180度)
更新:已解决。请参阅底部的修复。
在我支持所有方向的 iPad 应用程序中,如果应用程序以横向方式加载,则会发生奇怪的事情:
- 警报视图以纵向方式显示,即使其余部分处于横向方式
- 将 iPad 从横向旋转到纵向不会触发旋转(也不会触发旋转)。触发任何 shouldAutorotate 风格的方法)。
- 将 iPad 旋转 180 度(至“其他横向”)确实会触发旋转,之后,iPad 的行为与正常情况一样(即旋转时正确旋转至纵向和横向)。
下面的警报视图屏幕。奇怪,奇怪。任何想法为什么会发生这种情况?只有在横向加载时才会发生这种情况 - 以纵向加载然后旋转到横向效果很好。
解决方案: 出现此问题是因为我在顶部添加了 UIViewController (模态)应用程序完成加载后立即调用主视图控制器(更具体地说,在应用程序的 viewDidAppear:
方法中)。
iOS 在启动后向当前/主视图控制器发送许多 (4) 个 shouldAutorotateToInterfaceOrientation:
调用,以确定当前支持哪些方向。当我这么快添加另一个 VC 时,这发生在上述调用的中间。因此,我只接到一两个电话(“肖像”,因为它以肖像开头)。
然后,主视图控制器对横向方向默认支持“否”,但新添加的视图控制器(也询问方向)现在高兴地回答“是”。
如果我删除了另一个 VC(横向),我会意识到我的主视图控制器卡在纵向中。所以我有一个肖像视图,上面有一个风景视图。警报等关注了前者的方向,因此导致了上述灾难性的结果。
此外,如果我尝试旋转到纵向,我不能,因为主视图控制器已经处于纵向。旋转到横向也是不可能的,因为另一个视图控制器已经处于横向状态。只有当我进行“横向但另一个”旋转时,两个视图控制器才同意需要旋转,因此设备旋转。
在我的例子中,解决方法是简单地稍微延迟另一个控制器的创建。使用 performSelector:withObject:afterDelay.
-- 0 似乎可以工作,但我只是将其设置为 1 以确保。看起来有点奇怪,先出现了主 vc,然后出现了其他 vc,但比上面的要好。
Update: Solved. See fix at bottom.
In my iPad app which supports all orientations, if the app is loaded in landscape, weird things happen:
- Alert views appear in portrait, even though the rest is in landscape
- Rotating the iPad from landscape to portrait does not trigger a rotation (nor does it trigger any of the shouldAutorotate style methods).
- Rotating the iPad 180 degrees (to "other landscape") does trigger rotation, and after this, the iPad behaves like normal (i.e. rotates correctly to portrait and landscape when rotated).
Alert view screen below. Weird, weird. Any ideas why this might be happening? It only happens when loading in landscape -- loading in portrait and then rotating to landscape works just fine.
Solution: This problem occurred because I was adding a UIViewController (modally) on top of the main view controller as soon as the app finished loading (more specifically, in the app's viewDidAppear:
method).
iOS sends a number of (4) shouldAutorotateToInterfaceOrientation:
calls to the current/main view controller after things have started up, to figure out which orientations are currently supported. When I added the other VC so soon, this occurred in the middle of the aforementioned calls. Thus, I only got one or two calls (for "portrait", because it starts with portrait).
The main view controller was then left with a default support of "NO" for the landscape orientations, but the newly added view controller (which is also asked about orientations) now happily responds yes.
If I had removed the other VC (which is in landscape), I would have realized that my main view controller was stuck in portrait. So I had a portrait view, on top of which there was a landscape view. The alerts and such heeded the orientation of the former, hence the disastrous results above.
Additionally, if I tried to rotate to portrait, I couldn't, because the main view controller was already in portrait. Rotating to landscape was also impossible because the other view controller is already in landscape. Only when I did the "landscape but that other one" rotation, did both view controllers agree that rotation was required, and thus the device rotated.
The fix in my case was to simply put the creation of the other controller at a slight delay. using performSelector:withObject:afterDelay.
-- 0 seemed to work but I just put it at 1 to be sure. It looks a bit weird, with the main vc appearing and then the other vc, but it's better than the above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
出现此问题是因为应用程序完成加载后(更具体地说,在应用程序的 viewDidAppear: 方法中),我在主视图控制器顶部添加了 UIViewController (模态)。
有关解决方案的详细信息,请参阅上面的帖子。
This problem occurred because I was adding a UIViewController (modally) on top of the main view controller as soon as the app finished loading (more specifically, in the app's viewDidAppear: method).
See above post for details on solution.
使用
UIAlertController
而不是UIAlertview
。Use
UIAlertController
instead ofUIAlertview
.