添加到 UIWindow 的 UIView 中的方向
我有一个 UIView,它应该覆盖整个设备(UIWindow)以支持我正在使用核心动画执行的图像放大/缩小效果,其中用户点击 UITableViewCell 上的按钮,然后我缩放关联的图像。
缩放执行完美,我无法弄清楚为什么即使设备处于横向状态,子视图仍处于纵向模式。如下图所示:
我确实有一个导航控制器,但此视图已直接添加到 UIWindow 中。
I have a UIView which is supposed to cover the whole device (UIWindow) to support an image zoom in/out effect I'm doing using core animation where a user taps a button on a UITableViewCell and I zoom the associated image.
The zooming is performing flawlessly, what I haven't been able to figure out is why the subview is still in portrait mode even though the device is in landscape. An illustration below:
I do have a navigation controller but this view has been added to the UIWindow directly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以在此处了解一些可能的原因:
技术问答 QA1688 - 为什么我的 UIViewController 不随设备旋转?
在您的情况下,您可能会将视图作为另一个子视图添加到窗口中。只有第一个子视图获取旋转事件。您可以做的是将其添加为第一个窗口子视图的子视图。
You can read about some of the possible causes here:
Technical Q&A QA1688 - Why won't my UIViewController rotate with the device?
In your situation its probably the fact that you are adding the view as another subview to the window. Only the first subview gets the rotation events. What you can do is add it as a subview of the first window subview.
问题
https://developer.apple.com/library/content/qa/ qa1688/_index.html
该解决方案
我开源了一个名为 AGWindowView 的 pod。
它会自动处理任何旋转和框架更改,因此您不必担心这一点。
该代码
支持SDK和iOS系统版本的任意组合。相关代码可以在这里找到:
https://github.com/hfossli/AGWindowView/blob/master/来源/AGWindowView.m
The problem
https://developer.apple.com/library/content/qa/qa1688/_index.html
The solution
I have open sourced a pod named AGWindowView.
It will automatically deal with any rotation and framechanges so you won't have to worry about that.
The code
It supports any combination of SDK's and iOS system versions. The relevant code can be found here:
https://github.com/hfossli/AGWindowView/blob/master/Source/AGWindowView.m
我在 UIApplication 上创建了一个类别,它具有用于获取 keyWindow 的第一个子视图的辅助属性和方法。无论如何,这就是您想要覆盖的视图。现在,当您将由 UIViewController 管理的视图添加到该视图时,将调用 shouldRotateToInterfaceOrientation: 方法。
UIApplication+WindowOverlay.h
UIApplication+WindowOverlay.m
以下是如何使用它。
I created a category on UIApplication that has a helper property and method for getting the first subview of the keyWindow. This is the view you want to overlay anyway. Now when you add a view that is managed by a UIViewController to that view, the shouldRotateToInterfaceOrientation: method is called.
UIApplication+WindowOverlay.h
UIApplication+WindowOverlay.m
and here is how you would use it.
这是因为正如您所提到的,您的视图已直接添加到 UIWindow 中,因此当为导航控制器调用旋转方法时,uiview 不会发生任何变化。如果 UIView 是视图控制器视图的子视图,它将旋转。如果由于某种原因无法做到这一点。然后你可以重写这个方法:
每次你的方向改变时也会改变你的视图方向。
This is because as you mention your view has been added directly to the UIWindow, therefore when the method to rotate is called for the navigation controller nothing happens to the uiview. The UIView would rotate if it was a subview of the view controller view. If for some reason this cannot be done. Then you could override this method:
And every time your orientation changes also change your view orientation.
我在将视图直接添加到窗口时遇到了类似的问题。也许这会有所帮助:添加到窗口后自动调整 UIView 的大小
I had a similar problem with views being added directly to a window. Maybe this will help: Automatically Sizing UIView after Adding to Window
我如何解决这个问题的另一个解决方案。
定义当前方向:
然后检查 viewWillLayoutSubviews 中的方向:
Another solution how I solved this problem.
Define the current Orientation:
Then check the orientation in the viewWillLayoutSubviews: